Microsoft Interview Question
Software Engineer / Developers#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
/*
* tokenize() is based on strsep()
* The only difference is how they handle the empty fields.
*/
bool match(char *c, const char *delim)
{
unsigned int len = strlen(delim);
for (unsigned int i = 0; i < len; i++) {
if (*c == *(delim + i))
return true;
}
return false;
}
char *tokenize(char **stringp, const char *delim)
{
char *p = *stringp;
// If *stringp is initially NULL, strsep() returns NULL.
if (*stringp == NULL)
return NULL;
unsigned int len = strlen(*stringp);
for (unsigned int i = 0; i < len; i++) {
/*
* locates, in the string referenced by *stringp, the
* first occurrence of any character in the string delim
* and replaces it with a '\0'
*/
if (match(p + i, delim)) {
*(p + i) = '\0';
for (unsigned int j = 1; j < len - i - 1; j++) {
/*
* The location of the next non-delimiter character after
* the delimiter character (or NULL, if the end of the
* string was reached) is stored in *stringp.
*/
if (!match(p + i + j, delim)) {
*stringp = (p + i + j);
break;
}
}
break;
}
}
if (*stringp == p)
*stringp = NULL;
// The original value of *stringp is returned.
return p;
}
int main()
{
char *token, *string, *tofree;
tofree = string = strdup("abc def ,ghi");
assert(string != NULL);
while ((token = tokenize(&string, " ,")) != NULL) {
printf("%s\n", token);
}
free(tofree);
}
1. Treat String as a character array
2. Use a finite state machine while int nextChar is less than the string length and a temporary character array of a set size to hold the current token.
3. in the finite state machine: while(string[nextChar] != ' ') { nextToken[nextTokenChar++] = string[nextChar++] }
4. When the nextChar does equal ' ' , place null terminator on nextToken buffer and add it to a linked list of tokens and then increment the nextChar until string[nextChar] != ' ' (thus eliminating the worry about repeating white spaces.
<pre lang="" line="1" title="CodeMonkey48883" class="run-this">/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
/**
*
* @author USE
*/
public class StringToken {
public String[] splitStrings(char[] c) {
StringBuffer sb = new StringBuffer();
int index = 0;
String[] str = new String[10];
if (c.length == 0) {
return null;
}
for (int i = 0; i < c.length; i++) {
if (c[i] == ' ')
if (c[i+1] == c[i]) { //multiple blank spaces
continue;
} else { //single blank space
// allocate the created stringbuffer to newly created string and
//increment the index
str[index] = new String(); // create a new string
str[index] = sb.toString();
sb = new StringBuffer();
index++;
}
else {
sb.append(c[i]);
}
str[index]= sb.toString();
} //end of for loop
return str ;
}
public void validateToString(){
char [] x = new char[]{'T','h','i','s',' ',' ','i','s',' ',' ',' ',' ','L','A','L','I','T'};
String [] str = splitStrings(x);
for(int i=0;i<str.length;i++){
if(str[i]!=null)System.out.println(str[i]);
}
}
public static void main(String agrs[]){
new StringToken().validateToString();
}
}
</pre><pre title="CodeMonkey48883" input="yes">
</pre>
www dot testskillshome dot com/?p=2286
- Mister March 10, 2011