Showing posts with label How to Capitalize Words First Character in C. Show all posts
Showing posts with label How to Capitalize Words First Character in C. Show all posts

Wednesday, 4 February 2015

Capitalizing Words First Character in C++

Structure of the Problem Requirements  

This Program will Capitalize the the words first letter in a string. For this we used C++ builtin functions isupper and isalpha. The user will enter a string and the program capitalized all the first characters of words with some logic which we define in condition .

Source Code

#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
int main()
{
const int Maxlength = 500;
char sentence[Maxlength];
char a = true;
cout << " Enter Your Story \n ";
cout<<endl;
cin.getline(sentence, Maxlength);
for (int i = 0; i < *(sentence); i++)
{
if (isalpha(sentence[i]) && a == true)
{
sentence[i] = toupper(sentence[i]);
a = false;
}

if (isspace (sentence[i+1]))
a = true;
}
cout<<endl;
cout <<" After Capitalizing the First Character of All Words now Your Story is \n \n ";
cout<<endl;
cout << sentence << endl;

return 0;
}

Output of the Program

Capitalizing Words First Character in C++