Counting a character with strings

This is a discussion on Counting a character with strings within the C Programming forums, part of the General Programming Boards category; Im having trouble getting this program to work...I have a string "How do you know" ...and i have to count ...

  1. #1
    Unregistered
    Guest

    Counting a character with strings

    Im having trouble getting this program to work...I have a string "How do you know" ...and i have to count how many 'o's are in the string .... heres my source can anybody help me....thanx...

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

    int main (void)
    {
    char *strng = "How do you know";
    char *pToken;
    int tokenCount;

    tokenCount = 0;

    pToken = strchr(strng,'o');

    while (pToken)
    {
    tokenCount++;
    }

    printf("%2d",tokenCount);

    return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    9

    Talking answer

    modify this code and you will get the answer

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

    int main (void)
    {
    char *strng = "How do you know";
    char *pToken;
    int tokenCount;

    tokenCount = 0;

    pToken = strchr(strng,'o');

    while (pToken)
    {
    tokenCount++;
    strng=pToken++;
    pToken = strchr(strng,'o');

    }

    printf("%2d",tokenCount);

    return 0;
    }

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    6
    it still doesnt seem to print the number of 'o's it found in the string .....

  4. #4
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void)
    {
    	char *str = "How do you know";
    	char *ptr;
    	int counter = 0;
    
    	while (ptr = strchr (str, 'o'))
    	{
    		counter++;
    		str = ptr + 1;
    	}
    
    	printf ("%d\n", counter);
    
    	return 0;
    }
    CAUTION: This program changes the value of str !!

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    Try this version:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char *temp,*str="How do you know?";
      int count=0;
    
      temp=str;
      
      while(*str!='\0')
        {
           if (*str=='o' || *str=='O') count++;
        }
    
      str=temp;
    
      printf("number of oīs in \"%s\": %d\n", count, str);
    
      return 0;
    }
    klausi
    When I close my eyes nobody can see me...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character strings
    By campermama in forum C++ Programming
    Replies: 1
    Last Post: 06-17-2004, 04:26 PM
  2. printing strings (was similar problem)
    By weirdbeardmt in forum C Programming
    Replies: 5
    Last Post: 06-01-2004, 01:12 PM
  3. character strings and reading in a file...
    By Unregistered in forum C Programming
    Replies: 14
    Last Post: 07-30-2002, 09:51 AM
  4. Character counting program
    By TankCDR in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2002, 09:01 PM
  5. Array of character strings
    By nima_ranjbar in forum C++ Programming
    Replies: 1
    Last Post: 02-08-2002, 08:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21