C ++ is used to read an English text and uppercase the first letter of each English word in the text.
In this program, we read the stream from a text and use the fstream stream. During text conversion, the isalpha () -- whether it is a letter, toupper () -- is used to convert it to uppercase characters (operations on a single character of the string object) are used. Similar operations include 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 be printed, islower () -- whether it is a lowercase letter, isprint () -- whether it is a printable character, ispunct () -- whether it is a punctuation, isspace () -- whether it is a space, isupper () -- whether it is a capital letter, isxdigit () -- whether it is a hexadecimal number, tolower () -- convert to lowercase.
Copy codeThe Code is 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 the File Reading Function
Ifs. open ("d: \ com.txt", ios: in); // in -- open the file for read Operations
The content in cout <"d :\\ com.txt" <"is as follows:" <endl;
While (! Ifs. eof () // determines whether the stream ends.
{
Ifs. getline (buffer, 500, '\ n'); // The number of characters reaches 256 or ends when a line break occurs.
Str = buffer;
If (str. empty () // skip if a behavior is empty
{
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 its front is not a letter, it is capitalized
If (isalpha (str [index]) &! Isalpha (str [index-1])
{
Str [index] = toupper (str [index]); // note that a value must be assigned after conversion.
}
}
}
Cout <str <endl;
}
Ifs. close ();
}