c - String formatting, strtok issues -


i'm working on program separates words in string, , prints each word on different line. having difficulty output. ex.

"this string" 

prints

""this  "is"   "a"  "string"  " 

instead of

"this" "is" "a"  "string" 

code:

#include <string.h> #include <stdio.h>  void wholestring(char s[]) {    int i;    (i=0; i<strlen(s); ++i) {       }    return; }  int main(){     const int mysize = 100;     char mystr[mysize];     char *newstr;      fgets(mystr, mysize, stdin);     wholestring(mystr);      newstr = strtok (mystr, " ");      while (newstr != '\0'){         printf ("\"%s\" \n", newstr);         newstr = strtok ('\0', " ");     }     return 0; } 

there 2 problems seeing in code

newstr = strtok (mystr, " "); 

this have issues if input within quotes example "this string"

instead of

this string

the

""this  <-- here "is"   "a"  "string"  " 

is because of input within quotes ""

this can cleared

newstr = strtok (mystr, "\"| "); 

the other next line character @ end of buffer needs cleared null, other wise bound exitra newlines or quotes in here

""this  "is"  "a"  "string"  " <-- here  

sorry incomplete answer

fgets(mystr, mysize, stdin); wholestring(mystr);  /** ensure next line no more available **/  if(mystr[strlen(mystr)-1] == '\n')      mystr[strlen(mystr)-1] = '\0';      newstr = strtok (mystr, "\"| "); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -