Thread: string functions (beginner)

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    3

    string functions (beginner)

    I've been agonizing over this all day. The assignment is to capitalize every other word in a user input string.

    My logic is as follows:
    1. I have the program get each character until it encounters white space (using for loop)
    2. Then it should capitalize each character after the white space until it encounters another white space (using while loop).

    My problem though is when i try to create a condition for while loop i have to terminate is when white space is encountered, but the very first character to start the while loop is a white space..

    I was told to define boolean values, but after trying a few boolean conditions I'm still stuck..

    Thank you!!

    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    using namespace std;
    
    
    int main ()
    {
    char nextChar;
    int i;
    bool space;
    string string1;
    cout<<"Enter a string:\n";
    getline(cin,string1);
    for(i=0;i<string1.length();i++)
    {
    nextChar=string1.at(i);//get a character
    if(isspace(nextChar))
    {
    space=true;
    while() ---> STUCK HERE
    {
    string1[i]=toupper(string1.at(i));
    }
    }
    else
    {
    
    
    }
    }
    cout<<string1<<endl;
    return 0;
    }

  2. #2
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by annaz View Post
    I've been agonizing over this all day. The assignment is to capitalize every other word in a user input string.

    My logic is as follows:
    1. I have the program get each character until it encounters white space (using for loop)
    2. Then it should capitalize each character after the white space until it encounters another white space (using while loop).

    My problem though is when i try to create a condition for while loop i have to terminate is when white space is encountered, but the very first character to start the while loop is a white space..

    I was told to define boolean values, but after trying a few boolean conditions I'm still stuck..

    Thank you!!

    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    using namespace std;
    
    
    int main ()
    {
    char nextChar;
    int i;
    bool space;
    string string1;
    cout<<"Enter a string:\n";
    getline(cin,string1);
    for(i=0;i<string1.length();i++)
    {
    nextChar=string1.at(i);//get a character
    if(isspace(nextChar))
    {
    space=true;
    while() ---> STUCK HERE
    {
    string1[i]=toupper(string1.at(i));
    }
    }
    else
    {
    
    
    }
    }
    cout<<string1<<endl;
    return 0;
    }
    You don't really need the while loop (although you could do it that way). Since the Boolean 'space' is defined outside the loop, and survives over multiple iterations, you could set a condition check at the beginning to see if the loop has detected a space, and if the character satisfies isalpha():

    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    
    using namespace std;
    
    int main ()
    {
        char nextChar;
        int i;
        bool space = false; // Maybe set to true for a different effect on first word
        
        string string1;cout<<"Enter a string:\n";
        
        getline(cin,string1);
    
        for(i=0; i<string1.length(); i++)
        {
            // If there was a space, and if the current char is a letter
            if( space && isalpha( string1.at(i) ) )
            {
                string1[i] = toupper( string1.at(i) );
    
                space = false;
            }
    
            nextChar=string1.at(i);
    
            //get a character
            if(isspace(nextChar))
            {
                space=true;
            }
        }
    
        cout<<string1<<endl;
    
        return 0;
    }
    There are multiple ways to do this, and you should probably work a few out in full just for practice. Hope this helps you do that .

    Edit: Also check out the string class's functions, such as string::find().

    http://www.cplusplus.com/reference/string/string/find/
    Last edited by Alpo; 11-14-2014 at 06:10 PM.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  3. #3
    Registered User
    Join Date
    Nov 2014
    Posts
    3
    Thank you so much! That was a very quick response.

    I might have not explained the problem correctly. For input: "This is a random string" the output should be: "This IS a RANDOM string."

    I'm not sure I understand the lines

    Code:
      // If there was a space, and if the current char is a letter
            if(space && isalpha( string1.at(i) ) )
            {
                string1[i] = toupper( string1.at(i) );
     
                space = false;
            }
    Am I understanding correctly that bool "space" was initialized as false earlier? So its value is set to 0. And doesn't AND operator only produce true value is both operands are equal to 1?

  4. #4
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by annaz View Post
    Thank you so much! That was a very quick response.

    I might have not explained the problem correctly. For input: "This is a random string" the output should be: "This IS a RANDOM string."

    I'm not sure I understand the lines

    Code:
      // If there was a space, and if the current char is a letter
            if(space && isalpha( string1.at(i) ) )
            {
                string1[i] = toupper( string1.at(i) );
     
                space = false;
            }
    Am I understanding correctly that bool "space" was initialized as false earlier? So its value is set to 0. And doesn't AND operator only produce true value is both operands are equal to 1?
    I'm sorry I misunderstood your requirements. I wrote that thinking you needed the first letter of every word capitalized. If YOU want A string LIKE this, you might want to dispense with the Boolean value, and just use an integer. Spaces aren't a thing you need to keep track of, just skip them. The only thing you need to keep track of is the word count.

    Increment an integer every time your loop detects the first letter of every word, then depending on if the count is even or odd, you can treat that segment differently:


    Code:
     
        for(i=0;i<string1.length();i++)
        {
            //get a character
            nextChar=string1.at(i);
    
            // Skip all spaces. 
            if(isspace(nextChar))
            {
                continue;
            }
    
            else if( isalpha( nextChar ) )
            {
                // wordCount starts at 0, and is incremented every time you find a letter.
                wordCount++;
    
                // The capitalization condition with loop for every other word.
                if( wordCount % 2 )
                {
                    // Loop until next space, capitalizing letters.
                    while( i < string1.length() && !isspace( string1.at( i ) ) )
                    {
                        string1[i] = toupper( string1.at( i ) );
    
                        i++;
                    }
                }
                else
                {
                    // Skip to either end of line or next space
                    while( i < string1.length() && !isspace( string1.at( i ) ) )
                    {
                        i++;
                    }
                }
            }
        }
    Alternatively, you could use a bool I believe, and just flip it's value every time you find a word.
    Last edited by Alpo; 11-14-2014 at 08:08 PM.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  5. #5
    Registered User
    Join Date
    Nov 2014
    Posts
    3
    Thank you so much Alpo! Would you recommend a resource to read about boolean values - everything I found, including my text book have nothing on "flipping" the values and I was looking for more examples..

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't think there's a text book on flipping values.
    But the point is that you can do:

    bool b = true;
    b = !b; // b = false
    b = !b; // b = true

    and so on.
    The idea is that you have some logic

    bool Capitalize = false;
    Find whitespace
    Capitalize = !Capitalize;
    Capitalize if necessary
    Find whitespace
    Capitalize = !Capitalize
    ...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with beginner pointer functions.
    By Mo777 in forum C Programming
    Replies: 9
    Last Post: 02-17-2011, 03:30 PM
  2. Beginner at Functions
    By patm95 in forum C Programming
    Replies: 4
    Last Post: 09-28-2008, 03:18 PM
  3. Need some help with a beginner functions program!
    By nelledawg in forum C Programming
    Replies: 5
    Last Post: 03-03-2008, 07:05 AM
  4. arrays and functions (beginner q)
    By eazhar in forum C++ Programming
    Replies: 4
    Last Post: 07-13-2002, 05:39 AM
  5. Beginner who is lost - functions
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 04-07-2002, 08:27 PM

Tags for this Thread