C++ program that accepts somebody's name in any case then outputs it in the proper case
#include <iostream>
#include <string>
using namespace std;
string nameToProperCase(string name) {
for (int x = 0; x < name.length(); x++)
{
if (x == 0)
{
name[x] = toupper(name[x]);
}
else if (name[x - 1] == ' ')
{
name[x] = tolower(name[x]);
}else{
name[x] = tolower(name[x]);
}
}
return name;
}
int main()
{
string name;
cout<<"Enter name: ";
getline(cin,name);
cout<<"The proper case: "<<nameToProperCase(name)<<"\n";
system("pause");
return 0;
}
Comments
Leave a comment