Using C + +, read an English text and capitalize the first letter of each English word in the text.
This program has practiced reading a stream from a text, using the FStream flow. During the conversion of the text, the Isalpha ()-whether it is a letter, ToUpper ()-is converted to uppercase characters-the operation of a single character for a string object. A similar operation also has isalnum ()-whether it is a letter or number, Iscntrl ()-Whether it is a control character, IsDigit ()-Whether it is a number, isgraph ()-Whether it is not a space, but can print, islower ()-whether it is a lowercase letter, Isprint ()-Whether it is a printable character, ispunct ()-whether it is a punctuation mark, isspace ()-whether it is a space, isupper ()-Uppercase, Isxdigit ()-whether it is a hexadecimal number, tolower ()-- Convert to lowercase.
Copy Code code as follows:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace Std;
int _tmain (int argc, _tchar* argv[])
{
Read the file to the console
Char buffer[500];
String str;
Ifstream IFS; Provides read-file functionality
Ifs.open ("D:\\com.txt", ios::in);//in--open file for read operation
The contents of cout << "D:\\com.txt" << "are as follows:" << Endl;
while (!ifs.eof ())//Determine if the end of the stream is reached
{
Ifs.getline (buffer,, ' \ n '); The character reaches 256 or ends when it encounters a newline.
str = buffer;
if (Str.empty ())//If a behavior is empty, skip
{
Continue
}
Else
{
if (Isalpha (str[0]))
{
Str[0] = ToUpper (str[0]);
}
for (String::size_type index = 1; index!= str.size (); index++)
{
Str[index] is a letter, and before it is not a letter, uppercase
if (Isalpha (Str[index]) &&!isalpha (str[index-1))
{
Str[index] = ToUpper (Str[index]); Note that after this transformation, you have to assign a value
}
}
}
cout << str << Endl;
}
Ifs.close ();
}