Convert the first letter of each word to uppercase _c language using the C + + implementation

Source: Internet
Author: User
Tags lowercase
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 ();
}

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.