problem no 32: string problem:Upper case conversion

 QUESTION:

Given a string str, convert the first letter of each word in the string to uppercase. 

Example 1:

Input:
str = "i love programming"
Output: "I Love Programming"
Explanation:
'I', 'L', 'P' are the first letters of 
the three words.

 

ANSWER:

 

string transform(string s)
{
    string ss;
    int ascii=(int)s[0];
    char charac=(char)(ascii-32);
 
    ss=ss+charac;
 
    for(int i=1;i<s.size();i++)
    {
        if(s[i-1]==' ')
        {
            ascii=(int)s[i];
            charac=(char)(ascii-32);
            ss=ss+charac;
        }
        else
        {
            ss=ss+s[i];
        }
    }
    return ss;
}


Comments

Popular posts from this blog

problem 3: given two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.

problem no 7:Given two numbers A and B, find Kth digit from right of AB.

Problem no 16: count the number of squares below N.