C++字符串输入数字输出
2015-06-19 11:05
330 查看
#include <iostream> #include <string.h> using namespace std; const char str[][4] = {"yi","er","san","si","wu","liu","qi","ba","jiu"}; const int num[] = {2,2,3,2,2,3,2,2,3};//寻找对应的字符串的大小。 int GetIndex(char *s) { int i = 0; for (;i<9;i++) { if (strncmp(str[i], s,strlen(str[i])) == 0)return i; } } void Grial(char *str) { char *p = str; char *s = new char[11]; int k = 0; while (*p != '\0') { int index = GetIndex(p); p += num[index];//跳过已经找到的这个数字。 s[k++] = index + '1'; s[k] = '\0'; } cout << s << endl;//打印。 } int main() { char *p = new char[100]; cin >> p; Grial(p); return 0; }
相关文章推荐
- C++12.4.1 构造函数(与类名相同,没有返回类型)----有初始化列表的构造函数
- C++ 构造函数,赋值构造函数,析构函数,赋值构造函数的调用次序
- Effective C++条款1
- 6.824 Lab4: Cache Locks回顾
- C++类使用构造函数初始化类表和构造函数函数体中赋值的区别
- 如何成为一个牛逼的C/C++程序员?
- Voronoi图简介及C语言实现
- C/C++程序员必须熟练应用的开源项目
- 游戏引擎开发之音乐播放(一)
- C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
- LeetCode 201. Bitwise AND of Numbers Range
- C++构造函数public,protected,private
- 几种C++ std::string和std::wstring相互转换的转换方法
- c++:变量,数组和指针
- c++求数组中的最小(大)的n位数
- 设计模式----------------简单工厂模式
- Effective C++ 条款1
- c++函数必须先声明在使用
- c++命名空间使用
- Ubuntu下 vi编写第一个C语言程序