Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string in C every white space

Tags:

c

I want to write a program in C that displays each word of a whole sentence (taken as input) at a seperate line. This is what i have done so far:


void manipulate(char *buffer); int get_words(char *buffer);  int main(){     char buff[100];      printf("sizeof %d\nstrlen %d\n", sizeof(buff), strlen(buff));   // Debugging reasons      bzero(buff, sizeof(buff));      printf("Give me the text:\n");     fgets(buff, sizeof(buff), stdin);      manipulate(buff);     return 0; }  int get_words(char *buffer){                                        // Function that gets the word count, by counting the spaces.     int count;     int wordcount = 0;     char ch;      for (count = 0; count < strlen(buffer); count ++){         ch = buffer[count];         if((isblank(ch)) || (buffer[count] == '\0')){                   // if the character is blank, or null byte add 1 to the wordcounter             wordcount += 1;         }     }     printf("%d\n\n", wordcount);     return wordcount; }  void manipulate(char *buffer){     int words = get_words(buffer);     char *newbuff[words];     char *ptr;     int count = 0;     int count2 = 0;     char ch = '\n';      ptr = buffer;     bzero(newbuff, sizeof(newbuff));      for (count = 0; count < 100; count ++){         ch = buffer[count];         if (isblank(ch) || buffer[count] == '\0'){             buffer[count] = '\0';             if((newbuff[count2] = (char *)malloc(strlen(buffer))) == NULL) {                 printf("MALLOC ERROR!\n");                 exit(-1);             }             strcpy(newbuff[count2], ptr);             printf("\n%s\n",newbuff[count2]);             ptr = &buffer[count + 1];             count2 ++;         }     } } 

Although the output is what i want, i have really many black spaces after the final word displayed, and the malloc() returns NULL so the MALLOC ERROR! is displayed in the end. I can understand that there is a mistake at my malloc() implementation but i do not know what it is.

Is there another more elegant - generally better way to do it?

Thanks in advance.

like image 590
redsolja Avatar asked Dec 22 '10 20:12

redsolja


People also ask

How do you split a string with white space characters?

You can split a String by whitespaces or tabs in Java by using the split() method of java. lang. String class. This method accepts a regular expression and you can pass a regex matching with whitespace to split the String where words are separated by spaces.

Can I split a string in C?

In C, the strtok() function is used to split a string into a series of tokens based on a particular delimiter. A token is a substring extracted from the original string.

How do you split in white space?

The standard solution to split a string is using the split() method provided by the String class. It accepts a regular expression as a delimiter and returns a string array. To split on any whitespace character, you can use the predefined character class \s that represents a whitespace character.

How split a string by space in C#?

To split a string we need delimiters - delimiters are characters which will be used to split the string. Suppose, we've the following string and we want to extract the individual words. char str[] = "strtok needs to be called several times to split a string"; The words are separated by space.


2 Answers

http://www.cplusplus.com/reference/clibrary/cstring/strtok/

Take a look at this, and use whitespace characters as the delimiter. If you need more hints let me know.

From the website:

char * strtok ( char * str, const char * delimiters ); 

On a first call, the function expects a C string as argument for str, whose first character is used as the starting location to scan for tokens. In subsequent calls, the function expects a null pointer and uses the position right after the end of last token as the new starting location for scanning.

Once the terminating null character of str is found in a call to strtok, all subsequent calls to this function (with a null pointer as the first argument) return a null pointer.

Parameters

  • str
    • C string to truncate.
    • Notice that this string is modified by being broken into smaller strings (tokens). Alternativelly [sic], a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended.
  • delimiters
    • C string containing the delimiter characters.
    • These may vary from one call to another.

Return Value

A pointer to the last token found in string. A null pointer is returned if there are no tokens left to retrieve.

Example

/* strtok example */ #include <stdio.h> #include <string.h>  int main () {   char str[] ="- This, a sample string.";   char * pch;   printf ("Splitting string \"%s\" into tokens:\n",str);   pch = strtok (str," ,.-");   while (pch != NULL)   {     printf ("%s\n",pch);     pch = strtok (NULL, " ,.-");   }   return 0; } 
like image 160
Hortinstein Avatar answered Sep 24 '22 13:09

Hortinstein


For the fun of it here's an implementation based on the callback approach:

const char* find(const char* s,                  const char* e,                  int (*pred)(char)) {     while( s != e && !pred(*s) ) ++s;     return s; }  void split_on_ws(const char* s,                  const char* e,                  void (*callback)(const char*, const char*)) {     const char* p = s;     while( s != e ) {         s = find(s, e, isspace);         callback(p, s);         p = s = find(s, e, isnotspace);     } }  void handle_word(const char* s, const char* e) {     // handle the word that starts at s and ends at e }  int main() {     split_on_ws(some_str, some_str + strlen(some_str), handle_word); } 
like image 38
wilhelmtell Avatar answered Sep 23 '22 13:09

wilhelmtell