Thread: skipping spaces...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    51

    skipping spaces...

    im writing a program that tokenizes a sentance and then tells how many letters / punctuation marks are in the sentance.

    my problem lies where i try to count the letters. when ever my program hits a space it stops. so in essance, its only hitting the first word

    here is my whole code.


    Code:
    #include <iostream>
    using namespace std;
    #include <iomanip>
    using std::skipws;  
    
    #include <cstring>
    
    int main()
    {
    
    
    
    char phrase[50] = {'\0'};
    char *tokenPtr;
    int x;
    int y = 0;
    int count = 0;
    int count2 = 0;
    cout<<"Enter a phrase to be counted and what ever else..\n";
    cin.getline (phrase , 50);
    
    cout<<"The sentance entered is....\n";
    cout<<phrase<<endl;
    
    
    
    tokenPtr = strtok(phrase, " ");
    
    while(tokenPtr != NULL)
    {
    tokenPtr = strtok(NULL , " ");
    count++;
    }
    
    cout<<"there are "<<count<<" words in your phrase \n";
    
     
    
    for (int i = 0; i <50; i++)
    {
    	if(phrase[i] == '\0')
    	{
    		break;
    
    	}
    count2++;
    }
    
    cout<<"number of letters in the phrase is "<<count2<<endl;
    
    
    
    return 0;
    
    
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The strtok() function places \0 into your array after every word, so when you do this later on:
    if(phrase[i] == '\0')
    you exit the loop after the first word.

    To solve the problem, either avoid strtok() or make a second copy of your array before letting strtok() mangle it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    51

    nope...

    i tried what you said, and i made a copy, but now everytime i run it, it just says my phrase has 50 letters in it(which is the number of elements in both arrays]

    if you could post some working examples, maybe i could better understand what you are showin me

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Does this help.
    Code:
    #include <iostream>
    #include <cctype> // for isalnum()
    #include <cstring> // for strlen()
    
    using namespace std;
    
    int main(void)
    {
      char s[50] = "This is a test";
      int len;
      cout <<s <<endl;
      cout <<"chars in the array: " <<sizeof (s) <<endl;
      cout <<"strlen(s): " <<(len =strlen(s)) <<endl;
    
      int Counter = 0;  
      for (int i = 0; i < len; i++)
      {
        if (isalnum(s[i])) Counter++;
      }
      
      cout <<"Letters/Numbers : " <<Counter <<endl;
      return(0);
    }
    
    
    /*
    This is a test
    chars in the array: 50
    strlen(s): 14
    Letters/Numbers : 11
    
    */
    To make a copy of the string, you need to use strcpy(). Did you do that?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing 3 spaces to tabs
    By dnguyen1022 in forum C Programming
    Replies: 2
    Last Post: 12-22-2008, 12:51 AM
  2. saving a CString to binary file with trailing spaces
    By nineofhearts in forum C++ Programming
    Replies: 12
    Last Post: 01-03-2006, 11:53 AM
  3. Handling spaces in command line arguments
    By starkhorn in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2004, 02:42 PM
  4. Replies: 5
    Last Post: 06-30-2003, 12:52 PM
  5. Skipping spaces
    By [email protected] in forum C Programming
    Replies: 1
    Last Post: 09-29-2001, 12:11 AM