3

I am using strtok for converting string into individual words. I have done the following:

int main() {
  char target[100];
  char *t;
  scanf("%s",target);
  t = strtok(target," ");
  while (t!= NULL)
  {
    printf("<<%s>>\n", t);
    t = strtok (NULL, " ");
  }
  return 0;
}

The input is a string such as 'this is a string', the output I am getting is<<this>>.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Tehreem
  • 476
  • 9
  • 23
  • How about `for (t = strtok(target," "); t; t = strtok (NULL, " ")) printf("<<%s>>\n", t);` ? (even though I'm not a huge fan of `<<%s>>`) – David C. Rankin Jul 30 '16 at 06:25

3 Answers3

6

The way you have written scanf it will accept string till white space only

scanf("%s",target);

SO You need to change the way you take input from console

scanf("%99[^\n]",target);
gsamaras
  • 71,951
  • 46
  • 188
  • 305
Mohan
  • 1,871
  • 21
  • 34
3

Change:

scanf("%s",target);

to:

fgets(target, 100, stdin);

since the first won't stop when encounters the whitespace in your input.

Output:

this is a string
<<this>>
<<is>>
<<a>>
<<string
>>

Notice how the newline fgets() stores affects the output. You can simply discard it if you want, like this:

fgets(target, 100, stdin);
target[strlen(target) - 1] = '\0';

and now the output is:

this is a string
<<this>>
<<is>>
<<a>>
<<string>>
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • @DavidC.Rankin agree, that's why `fgets()` was the first one that came to my mind. – gsamaras Jul 30 '16 at 06:19
  • 1
    Yep, that's why I voted it up (and for the good explanation). – David C. Rankin Jul 30 '16 at 06:20
  • Aagh, I changed it like u said, to fgets, still same output.. only the 1st word. – Tehreem Jul 30 '16 at 06:24
  • 1
    @Tehreem update your question and copy paste the exact code you are using! I assume that you are on a terminal, but that shouldn't be an issue. Oh I now see you got an answer, glad about it. :) – gsamaras Jul 30 '16 at 06:28
  • This is the code i am using now, http://ideone.com/yZTChR and its working. Is it compulsory to set the limit to 99 as in %99? – Tehreem Jul 30 '16 at 06:36
  • 1
    @Tehreem and BatCoder, no. It's a good idea, since it safeguards from a large input. However, the code will work, as long as the user's input is less than 100 chars. – gsamaras Jul 30 '16 at 06:39
3

If you want to continue using scanf(), then you can use the below code snippet:

#include<stdio.h>
#include <string.h>

int main() {
   char target[100];
   char *t;
   //scanf("%s",target);
   scanf("%99[0-9a-zA-Z ]", target);
   printf("%s\n",target);
   t = strtok(target," ");
   while (t!= NULL)
   {
      printf("<<%s>>\n", t);
      t = strtok (NULL, " ");
   }
   return 0;
}

Working code here.

Just writing scanf("%s",target); will read the input only till the first white space; which is why you get only the first word as the output. By writing scanf("%99[0-9a-zA-Z ]", target);, you are reading 99 characters (including numbers 0-9, a-z or A-Z and white space) from the input stream.

Hope this is helpful.

Mohan
  • 1,871
  • 21
  • 34
abhishek_naik
  • 1,287
  • 2
  • 15
  • 27
  • Anything wrong with `"%99[^\n]"`? You can still use your *character class*, but you should really use the *field width modifier* to limit the characters read to what will fit in `target` `:)` – David C. Rankin Jul 30 '16 at 06:15
  • @DavidC.Rankin, it will read only `99` characters. The OP does not say that the input length is `<=99`. So I think a generic one would be more helpful. – abhishek_naik Jul 30 '16 at 06:20
  • 1
    Well, not quite `"%[0-9a-zA-Z ]"` will read as many characters as are input (without limit) as long as they are `0-9a-zA-Z `, that's why `"%99[0-9a-zA-Z ]"` would be an improvement. – David C. Rankin Jul 30 '16 at 06:22
  • Yep, that is much better. Also, you need not change your answer, but consider the differences between the `"[^\n]"` character class (everything up to the newline) and `"[0-9a-zA-Z ]"` (only *alphanum*). Both can have their place, but if the user entered a `\t` (or some other strange thing users do) the `"[0-9a-zA-Z ]"` wouldn't handle the character. – David C. Rankin Jul 30 '16 at 06:30
  • Thanks this works... and doing `"%99[0-9a-zA-Z ]"` takes in 99 characters of type alphanumeric only right? – Tehreem Jul 30 '16 at 06:30
  • @DavidC.Rankin, yes, I agree. Thank you for your guidance. :) – abhishek_naik Jul 30 '16 at 06:36