c++作业记录,统计字母&字符串翻转
2014-10-26 22:35
141 查看
题目:编写一个函数,统计一条英文句子的字母个数,在主程序中实现输入输出
下面的是字符串的翻转:
不过好奇怪, 我自己写的个问题,不懂
//解法一: #include <iostream> #include <string> using namespace std; int main() { string str; getline(cin, str); int count=0; for (int i=0; i<str.length(); i++) { if((str[i]>=65 && str[i]<=90) || (str[i]>=97 && str[i]<=122)) count++; } printf("%d\n", count); system("PAUSE"); return 0; }
//解法二: #include <iostream> using namespace std; int main() { char ch; int count=0; while (cin.get(ch), ch != '\n') { if (ch != ' ') { count++; } } cout<<count<<endl; system("PAUSE"); return 0; }
下面的是字符串的翻转:
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string str; getline(cin, str); reverse(str.begin(),str.end()); cout<<str<<endl; system("PAUSE"); return 0; }
不过好奇怪, 我自己写的个问题,不懂
#include <iostream> #include <string> using namespace std; void Reverse(string &s, int i, int j) { if ((i==j) || (i>j)) return ; char c; c = s[i]; s[i] = s[j]; s[j] = c; Reverse(s,i+1, j-1); // Reverse(s, i++, j--); 为什么写成这样就翻转不了 } void reverse(string &s) { Reverse(s,0,s.length()-1); } int main() { string a; getline(cin, a); reverse(a); cout<<a<<endl; system("PAUSE"); return 0; }
相关文章推荐
- 华为oj 字符串个数统计&&数字颠倒&&字符串翻转&&字符逆序&&求int型数据在内存中存储时1的个数
- 第十三周项目5-字符串的操作(统计字母 ' A ' 出现的次数)
- C++用map记录字符串,怎么用map<string,int>才会不超时
- Ex7_4编写函数,统计此字符串中字母、数字、空格和其他字符­的个数
- c++实验5--统计输出字符串中(大/小写)字母个数,数字个数及其它字符个数。
- 第13周 项目5-字符串的操作(1)-<统计字符串中大小写字母的个数>
- 【编程珠玑】第十五章--字符串:统计文本中单词数量(C++的Map&C的结构体实现&POJ2418)
- 不用字符串库函数,实现求字符串的长度和翻转字符串······
- G++编译时 , C++中的getline,获取到的字符串是带回车的,即'/r'
- C++字符串完全指引 && 字符编码笔记:ASCII、Unicode、UTF-8、UTF-16、UCS、BOM、Endian (合集)
- 统计字符串中出现字母的个数
- 字符串中统计某字母的次数等【Java Base】
- 20100706 学习记录:截取字符串最后一个字符 && asp textarea 多行数组分割处理
- matlab 字符串文件直方图统计字母个数
- Java中统计字符串中大写、小写字母以及非英文字母数的N种方法
- 20100730 学习记录:gridview页脚加合计 & 逆向查找字符串 & 点图片弹出窗口
- c++ 程序 统计一个字符串内的正整数和负整数个数
- C++实现统计字符串出现次数
- 字符串中统计某字母的次数等【Java Base】
- C++调用C#类库 &amp;&amp; 非托管C++字符串和托管字符串的转换