By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,118 Members | 1,133 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,118 IT Pros & Developers. It's quick & easy.

can you give me the keyword to represent a space in c language?

P: n/a
dear sir,
i am new to c language. i am developing a program on string function and i want to count the number of 'THE' and 'A' in the given string.
but if i am using 'SPACE BAR' to give space in my string then compiler is ignoring the rest of the following characters.
please guide me what to do??
Nov 16 '10 #1
Share this Question
Share on Google+
2 Replies


Markus
Expert 5K+
P: 6,050
Well, a look at the code would help.

Here's how I would do it:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. int main (int argc, char *argv[])
  6. {
  7.     char str[]  = "the thesaurus looked longingly towards the dictionary";
  8.     char *found;
  9.     int  n_the  = 0,
  10.          n_a    = 0;
  11.  
  12.     /* Search for "the" */
  13.     found = strstr(str, "the");
  14.     while (found != NULL)
  15.     {
  16.         n_the++;
  17.  
  18.         /* + 3 = strlen of "the" */
  19.         found = strstr(found + 3, "the");
  20.     }
  21.  
  22.     /* Search for "a" */
  23.     found = strstr(str, "a");
  24.     while (found != NULL)
  25.     {
  26.         n_a++;
  27.  
  28.         found = strstr(found + 1, "a");
  29.     }
  30.  
  31.      printf("n_the: %d\n", n_the);
  32.      printf("n_a:   %d\n", n_a);
  33.  
  34.     return 0;    
  35. }
  36.  
Nov 16 '10 #2

Banfa
Expert Mod 5K+
P: 8,916
I suspect the op is reading stdin using scanf and so is only getting single words because that function uses space as a separator when they really want to read the whole line including spaces.

The solution is to use fgets which will read an entire line because it does not use space as a separator.
Nov 17 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.