"三步反转"处理字符串

来源:互联网 发布:怎么进入网站的数据库 编辑:程序博客网 时间:2024/05/23 13:00

istringstream、ostringstream、stringstream 类介绍 .

http://www.cnblogs.com/gamesky/archive/2013/01/09/2852356.html

下面的输入处理比较常用:

#include <cstdio>#include <iostream>#include <string>#include <sstream>using namespace std ;int main(){    freopen("in.txt", "r", stdin);    string str, line;    while(getline(cin, line))    {        istringstream stream(line);        while(stream>>str)            cout<<str.c_str()<<endl;    }       return 0;}

输入
I am a student.
输出
I
am
a
student.

C++编程中关于输入的一些函数
cin、cin.get()、cin.getline()、getline()、gets()等函数的用法:http://www.cnblogs.com/wanghao111/archive/2009/09/05/1560822.html

c++中string类的详解
http://blog.csdn.net/yzl_rex/article/details/7839379

三步反转

#include <cstdio>#include <iostream>#include <string>#include <sstream>using namespace std ;// 反转字符串string reverse_string(string s){    int len = s.length();    int from = 0 ;    int to = len-1;    while(from < to)    {        char tmp = s[from];        s[from] = s[to];        s[to] = tmp;        from ++ ;        to --;    }    return s;}int main(){    //freopen("in.txt", "r", stdin);    string str, line;    while(getline(cin, line))    {        string rs = "";        istringstream stream(line);        int words_num = 0;        while(stream>>str)        {            if(words_num == 0)                rs += reverse_string(str) ;            else                rs += " " + reverse_string(str);            words_num ++ ;        }        cout << reverse_string(rs) << endl ;    }       return 0;}

输入1
I am a student.
输出1
student. a am I

输入2
How are you
输出2
you are How

0 0
原创粉丝点击