字符串倒序输出

来源:互联网 发布:ori什么软件 编辑:程序博客网 时间:2024/04/30 16:30

读入空格跳出,其余不变;getchar()单字符读入判断直接TLE;

用到scnaf( "%[^ ]", str );表示读入除‘ '以外的所有字符;

#include<iostream>#include<cstring>#include<cstdio>using namespace std;const int maxn = 1005;char str[ maxn ], ch[ 5 ], k;int main(){        while( scanf( "%[^ ]", str ) == 1 ){            ;            }    //printf( "1111111111" );k = strlen( str );        k = strlen( str );        for( int i = k - 1; i >= 0; --i )            cout << str[ i ];        cout << endl;                return 0;}


TLE

#include<iostream>#include<cstring>#include<cstdio>using namespace std;const int maxn = 1005;char str[ maxn ], ch, k;int main(){k = 0;while( ( ch = getchar() )!= ' ' ){str[ k++ ] = ch;}for( int i = k - 1; i >= 0; --i )cout << str[ i ];cout << endl;return 0;}


0 0