c++(字符串操作 1)
来源:互联网 发布:企业网站seo教程 编辑:程序博客网 时间:2024/05/20 19:48
Function: 统计一行字符串中 的单词个数,并将 每个单词首字母 变为大写字母
1 /* 2 * File: lineword.cpp 3 * ------------------ 4 * This program converts initial letter of every word in line to upper. 5 */ 6 7 #include <iostream> 8 #include <string> 9 10 std::string lineToWord(std::string line); 11 std::string wordToUpper(std::string word); 12 13 int num; //number of words in line 14 15 int main() 16 { 17 std::string line; 18 num = 0; 19 std::cout<< "please enter string message: "; 20 getline(std::cin, line); 21 std::cout<< "result:[ " << num << " words ] "<< lineToWord(line) << std::endl; 22 23 return 0; 24 } 25 26 /* 27 * Function: lineToWord 28 * Usage: string translation = lineToWord(word); 29 * --------------------------------------------- 30 * acquire every word in line. 31 */ 32 33 std::string lineToWord(std::string line) 34 { 35 std::string result = ""; 36 int start = -1; //是否为单词的首字母的标识符 37 for(int i = 0; i < line.length(); i++) 38 { 39 char ch = line[i]; 40 if(isalpha(ch)) //isalpha()函数,判断字符是否为一字母 41 { 42 if(start == -1) start = i; 43 } 44 else 45 { 46 if(start >= 0) 47 { 48 result += wordToUpper(line.substr(start, i - start)); //.substr(),截取输入行中的单词 49 start = -1; 50 } 51 result += ch; //加上非字母的其它字符 52 } 53 } 54 if(start >= 0) result += wordToUpper(line.substr(start)); //处理最后一个单词 55 return result; 56 } 57 58 /* 59 * Function: wordToUpper 60 * Usage: string word = wordToUpper(word); 61 * --------------------------------------- 62 * convert the initial letter of word to upper letter 63 */ 64 65 std::string wordToUpper(std::string word) 66 { 67 char ch = toupper(word[0]); //toupper(),转换为大写字母 68 word = ch + word.substr(1); 69 num++; 70 return word; 71 }
阅读全文
0 0
- c++(字符串操作 1)
- 字符串操作(C++)
- C字符串操作小结(1)
- C字符串操作(转)
- C字符串操作(转)
- 字符串操作(C语言 C++)
- c语言字符串-常用操作(1)
- C 语言--字符串操作函数--笔记(1)
- C字符串操作:字符串反转
- C/C++ 字符串操作1---循环移位
- C/C++流操作(文件,字符串)
- C语言的字符串操作(例)
- C语言字符串操作(转)
- C字符串操作小结(2)
- 字符串定义操作(C语言)
- C语言字符串操作(一)
- C/C++的字符串操作(一)
- 字符串操作--c语言
- 随机汉字
- pageContext.findAttribute()与pageContext.getAttribute()的区别
- elasticsearch5基本概念
- Class的继承
- bean:resource bean:include jsp:include
- c++(字符串操作 1)
- java连接各种数据库及配各种服务器数据池(更新中)
- 与其他的PDF编辑器相比,迅捷PDF编辑器实在好用太多了
- jquery获取值传值
- Rk3288核心板Cadence制作(一)之原理图设计及orcad常用快捷键
- <html:messages id="">标签 <html:errors/>标签
- 判断链表中是否有环、并找出环的入口
- Go语言学习之strings包(the way to go)
- [摘]actionMessages(),actionError()介绍