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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hussain raja

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

    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??
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Well, a look at the code would help.

    Here's how I would do it:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main (int argc, char *argv[])
    {
    	char str[]  = "the thesaurus looked longingly towards the dictionary";
    	char *found;
    	int  n_the  = 0,
    	     n_a    = 0;
    	     
    	/* Search for "the" */
    	found = strstr(str, "the");
    	while (found != NULL)
    	{
    		n_the++;
    		
    		/* + 3 = strlen of "the" */
    		found = strstr(found + 3, "the");
    	}
    	
    	/* Search for "a" */
    	found = strstr(str, "a");
    	while (found != NULL)
    	{
    		n_a++;
    		
    		found = strstr(found + 1, "a");
    	}
     	
     	printf("n_the: %d\n", n_the);
     	printf("n_a:   %d\n", n_a);
     	
    	return 0;	
    }
    Last edited by Markus; Nov 16 '10, 01:26 PM. Reason: Fixed

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      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.

      Comment

      Working...