Thread: c++ string question

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    13

    c++ string question

    hi there , i am doing a prg which determines the number of words in the string ..

    here goes the problem i have to solve

    a function is provided with an array of char representing a null-terminated string.This function determines the number of"words" in the string .A word is any series of char that does not contain a blank char.


    could u tell me if this correct or does it need any changes ..
    //************************************
    //**************************************
    #include<iomanip.h>
    #include<string>
    //**************************************
    const int MAX=5;
    typedef char LIST_CH[MAX];
    //**************************************
    //prototypes
    void string_len(LIST_CH str_len);
    //************************************************** ***********
    void main()
    {
    LIST_CH str_len;
    string_len(str_len);
    }
    //************************************************** ***********
    void string_len(LIST_CH str_len)
    {
    int ct,
    count = 0;
    cout << "enter words";
    for( ct = 0 ; ct < 5 ;++ct){
    cin >> str_len[ct] ;
    }
    do{
    for(ct = 0 ; ct < 5 ; ++ct)
    ++count;
    }while(!(str_len) == '\0');
    cout << " length" << count;
    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay. What is your question?

    Kuphryn

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    13
    well when i run the prg it is giving me length of 5 no matter what i type...

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    You are going through a for-loop without ever exiting it until ct reaches five.

    -----
    // problem

    // for(ct = 0 ; ct < 5 ; ++ct)
    // ++count;
    -----

    Kuphryn

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    13
    i am now trying doing this
    -----------------------------------------------------------------
    while(!(str_len) == '\0')
    ++coutn;

    cout << " length" << count;
    ---------------------------------------------------------------------
    now it is not giving me anything

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay. Here is one possible solution.

    -----
    // get data from ifstream
    std::string m_sData;
    std::getline(cin, m_sData);
    cin.clear();
    int nWords = 0;

    // user entered one or more characters

    if (m_sData.size() > 0)
    {
    for (int i = 0; i < m_sData.size(); ++i)
    {
    if (m_sData[i] == " ";
    ++nWords;
    }

    ++nWords;
    }

    cout << "User entered " << nWords << " word(s)." << endl;
    -----

    Kuphryn

  7. #7
    Unregistered
    Guest
    cout "enter a string of words terminated by a # sign."
    char buffer[300];
    cin.getline(buffer, 299, '#');
    int length = strlen(buffer);
    int count = 0;
    for(int i = 0; i < length; i++)
    {
    if(buffer[i] == ' ')
    ++count;
    }
    cout "the number of words in a string is roughly equal to the number of spaces in the string. Therefore the number of words entered is " << count;

    In other words you need to look at each char in the string as entered. Each time you find the character is a space character then increment a variable standing for the number of words found.

    Another problem is how you are going enter the input into a single string. The easiest way is to use getline() or get() with terminating character being newline or some other char of your choice if input can be on more that one line.

    The problem counting just spaces is that if you restrict input to a single sentence, then the number of words is one more than the number of spaces, not the same as the number of spaces. Also if routine sentence structure is allowed then the space count will be off as there are routinely two spaces between the end of one sentence and the start of another. Therefore counting spaces is an approximation of the number of words. You can do better with a more sophisticated protocol, but I don't think that's what you want.

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    13
    ----------------------------------------------------------
    cout "enter a string of words terminated by a # sign."
    char buffer[300];
    cin.getline(buffer, 299, '#');
    int length = strlen(buffer);
    int count = 0;
    for(int i = 0; i < length; i++)
    {
    if(buffer[i] == ' ')
    ++count;
    }
    --------------------------------------------------------------
    but when i compile this it is giving me an out put of 1
    even though i enter "strange#"

    should'nt it give an output of 8 instead of 1..

  9. #9
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    > but when i compile this it is giving me an out put of 1
    > even though i enter "strange#"
    > should'nt it give an output of 8 instead of 1..

    It increments the counter whenever whitespace is encountered. There isn't 8 whitespaces in "strange" is there? Besides, counter=0 after exection when strange#<newline> is input.

    However,
    Code:
    char buf[200];
    cin.getline(buf, 199); 
    unsigned int words=0, i=0, len=strlen(buf);
    while(i<len&&buf[i]==' ')i++;
    while(i<len){
        words++;
        while(i<len&&buf[i]!=' ')
            i++;
        while(i<len&&buf[i]==' ')i++;
    }
    //words is number of words in buf
    Last edited by raimo; 07-12-2002 at 12:13 PM.

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    13
    thank u.

  11. #11
    Unregistered
    Guest
    if you input

    strange#

    with my code, and you output the value of count you should get an output of 0 which is the value of count after completing the for loop as there are 0 spaces in the input. Since the number of words in a single sentence is one more than the number of spaces the number of words in strange# is 1, so the output you got is correct. In my code if you output the variable length, it should be 7 with input as above as the # will be discarded by getline() leaving the seven visible char. getiline() will add an invisible char, the null char terminating the string, as well, but that isn't counted by strlen().

    The difference between my code and Kuphryn's is that s/he used an ifstream to read input from a file, whereas I used an istream to read from keyboard. S/he used default newline char to stop input into the buffer, whereas I used a #. And his/her code (incorrectly----but probably is a typo) attempts to equate a string consisting of a single space rather than the space character
    to a given character in the buffer as the conditional of the if statement.

    I find the for loop easier to understand than the while loops written by Raimo, but that appears to work as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Reusing a string pointer question
    By chiefmonkey in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2009, 04:53 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM