Use c ++ to convert the first letter of each word in the text to uppercase

Source: Internet
Author: User

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 ();
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.