Home > front end >  Trying to Capitalize the first letter of the second word in string
Trying to Capitalize the first letter of the second word in string

Time:04-08

I am creating a program that can look through a given .txt file and match with the input regardless of the case-insensitive.
So I am trying to convert a given string like " TOM HaNks " to "Tom Hanks" The code below currently deletes the unneeded whitespace and capitalizes the first letter So " TOM HaNks " returns "Tom hanks", but I need the second word capital as well for it to match with anything in the .txt file.

int main()
    {
    string upTxt;
    cout << "\nPlease Enter the String to Convert into Lowercase  =  ";
    getline(cin, upTxt);
    
    while(!upTxt.empty() && isspace(*upTxt.begin()))
        upTxt.erase(upTxt.begin());

    while(!upTxt.empty() && isspace(*upTxt.rbegin()))
        upTxt.erase(upTxt.length()-1);
    for (int i = 0; i < upTxt.length(); i  )
    {   
        upTxt[i] = tolower(upTxt[i]);
       
    }   
    upTxt[0] = toupper(upTxt[0]);

    cout<< "\nThe Given String in Lowercase "<< upTxt << endl;
    cout<<""<<endl;
    
    return 0;
    }

CodePudding user response:

Check in the for loop, if the current processing character is space the next letter to be processed should be convert to upper case. Also, you don't need to convert the first character to upper case separately, since you are trimming the leading and trailing spaces, you can take care of it while processing string in the loop. You can do:

    bool caps = true;

    for (int i = 0; i < upTxt.length(); i  ) {   
        if (isspace(upTxt[i])) {
            caps = true;
        } else {
            upTxt[i] = caps ? toupper(upTxt[i]) : tolower(upTxt[i]);
            caps = false;
        }
    }   

CodePudding user response:

I modified for loop to detect next word (if space is found then next character belongs to next word) and accordingly to make it uppercase.

Remaining characters are converted to lowercase.

#include <iostream>
using namespace std;

int main() {
    string upTxt;
    cout << "\nPlease Enter the String to Convert into Lowercase  =  ";
    getline(cin, upTxt);
    
    while(!upTxt.empty() && isspace(*upTxt.begin()))
        upTxt.erase(upTxt.begin());

    while(!upTxt.empty() && isspace(*upTxt.rbegin()))
        upTxt.erase(upTxt.length()-1);

    for (int i = 0; i < upTxt.length(); i  ) {   
        /* Handle first character */
        if(i==0) {
            upTxt[i] = toupper(upTxt[i]);
        } else {
            /* if previous character is space then make character uppercase */
            if(isspace(upTxt[i-1])) {
                upTxt[i] = toupper(upTxt[i]);
            } else {
                /* Make every remaining character lowercase */
                upTxt[i] = tolower(upTxt[i]);
            }
        }
    }   

    // upTxt[0] = toupper(upTxt[0]); /* not needed. for loop deals with it */

    cout<< "\nThe Given String in Lowercase "<< upTxt << endl;
    cout<<""<<endl;
    
    return 0;
}

You can also replace for loop with following

for (int i = 0; i < upTxt.length(); i  ) {   
    if(i==0) {
        upTxt[i] = toupper(upTxt[i]);
    } else {
        upTxt[i] = isspace(upTxt[i-1])  ? toupper(upTxt[i]) : tolower(upTxt[i]);
    }
}
  •  Tags:  
  • c
  • Related