Help with strings

This is a discussion on Help with strings within the C++ Programming forums, part of the General Programming Boards category; I have a problem trying to call a fuction with the string and the colon (':') as arguments. It is ...

  1. #1
    Curwa
    Guest

    Help with strings

    I have a problem trying to call a fuction with the string and the colon (':') as arguments. It is suppose to return then count number of tokens seperated by the colon in the string. The output should be shown in main. Any help is aprreciated. Thanks.

    Code:
    int countToken(char theString[],':');
    int main(void)
    {
     //local declerations
     char theString[] = "abcd0001:groupno:userno:name:/bin/ksh";
     int tokenCount;
     //statements
    
     tokenCount = countToken(theString,':');
       cout << " Number of words in string are: " << tokenCount;
    
    }//main
    
    int countToken (char theString[],':')
    {   
         int count;
         while (theString)
         {
           if (theString[] == ':')
           count++;
         }//while
        
     return count;
    } //countToken

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >int countToken (char theString[],':')

    I don't understand this & don't think it's legal. You need to declare what type your token is, or it you known it's always going to be ':', don't pass it all.

    You need to do something like:

    Code:
    int countToken(char theString[], char token = ':')
    {   
      // Token defaults to ':' if none specified
    
      int count = 0;  // Initialise at zero
      int len = strlen(theString);
    
      for(int n = 0; n < len; n++)  // Loop from character zero to end of string
      {
        if (theString[n] == token) // Check characters one by one
          count++;
       }
        
      return count;
    }

  3. #3
    Curwa
    Guest
    I tried your code...it gave me less errors but i still get one error....It says, "Default argument value redeclared for parameter ' token ' "and shows the error right after the countToken function...???

    As well, is there another way i can count the length of a string without using strlen??? Please tell me how if you know.

    Here is the updated code ..
    Code:
    int countToken(char theString[],char token = ':');
    int main(void)
    {
     //local declerations
     char theString[] = "abcd0001:groupno:userno:name:/bin/ksh";
     char token=':';
     int count;
     count = countToken(theString,token);
      cout << " Number of words in string are: " << count;
    
    }//main
    
    int countToken(char theString[], char token = ':')
    {
      // Token defaults to ':' if none specified
    
      int count = 0;  // Initialise at zero
      int len = strlen(theString);
    
      for(int n = 0; n < len; n++)  // Loop from character zero to end of string
      {
        if (theString[n] == token) // Check characters one by one
          count++;
      }
    
      return count;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

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