need help with strings and str functions

This is a discussion on need help with strings and str functions within the C Programming forums, part of the General Programming Boards category; im working on a program for my advanced c programming class in college and im having trouble. in this program ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    13

    Question need help with strings and str functions

    im working on a program for my advanced c programming class in college and im having trouble. in this program we have to make a program that counts the number of repeated characters in a string. if there are 4 repeated chars it is supposed to print out the string with a * at the end. if it has 5 repeated chars it has to print the string with ** at the end. so far i've been working on a string "mississippi".


    Code:
    char string[] = "mississippi";
    char *token;
    int count=0;
    	
    	
    printf("%s \n", string);
    
    
    token = strchr(string, 's');
    printf("%s\n", token);
    count++;
    while(token != NULL){
    token = strchr(token+1, 's');
    printf("%s\n", token);
    count++;}
    
    printf("%d",count);
    with this i would like to see how many 's' characters there are in mississippi, but i keep getting 5 instead of 4 because it keeps counting null for some reason. any ideas? also on how to make it count each letter in a string?

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    right now you are incrementing count 5 times, once at every s and once at the character that is one passed the last s. even though after the last s you're token does equal NULL it still has to finish the loop and increment the count one last time, just add "if(token != NULL)" right before your count++ to prevent this from happeneing.

    Code:
    char string[] = "mississippi";
    char *token;
    int count=0;
    	
    	
    printf("%s \n", string);
    
    
    token = strchr(string, 's');
    printf("%s\n", token);
    count++;
    while(token != NULL){
    token = strchr(token+1, 's');
    printf("%s\n", token);
    if(token != NULL)
    count++;}
    
    printf("%d",count);

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    thx alot that helped. and how would i make it count each letter, like in the line token = strchr(string, 's'); where i have the 's' is there anythin i can change it to so i could put it in a loop to count the anoubt of m's i's, and so on?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    void foo(const char *text)
    {
       int i, count[CHAR_MAX] = {0};
       for ( ; *text; ++text )
       {
          ++count[*text];
       }
       for ( i = 0; i < CHAR_MAX; ++i )
       {
          if ( count[i] )
          {
             printf("'%c' : %d\n", i, count[i]);
          }
       }
    }
    
    int main(void)
    {
       foo("mississippi");
       return 0;
    }
    
    /* my output
    'i' : 4
    'm' : 1
    'p' : 2
    's' : 4
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    wow nice but one prob lol, we suposed to use str functions while doing that. like as seen above, got it to count for s with the strchr() function but need that code to cycle through all the charcters in the string

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So modify the check in the first loop to use a comparison to strlen for when it stops.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    what do u mean? just wonderin if u can make the char comparison from token = strchr(string, 's');... the 's' part change some how in a loop. need that to cycle through each of the letters and find out which repeats the most. then i need to strcat a * to the end of the string. im still suck on the finding which repeats the most

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Have a for loop that loops from 'A' to 'Z'. Then see many times you can call strchr() before it returns NULL.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  2. std::string::operator== fails for equal strings?
    By pheres in forum C++ Programming
    Replies: 5
    Last Post: 03-25-2008, 11:21 AM
  3. Trimming Strings
    By shiju in forum C Programming
    Replies: 6
    Last Post: 01-20-2004, 07:47 AM
  4. Comparing Strings
    By Perica in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2003, 10:41 PM
  5. strings strings strings str..
    By Linette in forum C++ Programming
    Replies: 1
    Last Post: 02-17-2002, 01:12 PM

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