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
Post a Comment