首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

逆向输出,该如何解决

2012-04-07 
逆向输出如何“this is a test”把它逆向输出 ? 是“siht si a tset”如果给出全部逆向更好了偶要代码哦[解决

逆向输出
如何“this is a test”

把它逆向输出 ? 是 “siht si a tset”

如果给出全部逆向更好了

偶要代码哦



[解决办法]
试着写了一下,吼吼...

C/C++ code
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){    int i;    char* src = "this is a test";    char* ptr = src + strlen(src) - 1;    for (i = strlen(src); i > 0; i--)    {        printf("%c", *ptr--);    }    system("pause");    return 0;}
[解决办法]
用栈来做.
[解决办法]
C/C++ code
// test2.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <string.h>#include <stdio.h>char string[] = "this is a test";char seps[]   = " ,\t\n";char *token;void main( void ){   printf( "%s\r\n", string );   token = strtok( string, seps );   while( token != NULL )   {        //printf( " %s\n", token );        int l = strlen( token );        for( int i = l - 1; i >= 0; i-- )        {            printf( "%c", *( token + i ) );        }        printf( "%c", 0x20 );        token = strtok( NULL, seps );   }}
[解决办法]
C/C++ code
#include <iostream>using namespace std; int main() {    char str[] = "this is a test";    int len = strlen(str)-1;    for(int i = 0; i<=len/2; i++)    {        char c = str[i];        str[i] = str[len-i];        str[len-i] = c;    }    cout<<str<<endl;    return 0;}
[解决办法]
支持小默~~~~~~~
[解决办法]
部分逆向:分割每个单词,再逆向
全部逆向:直接逆向输出不就完了
[解决办法]
while(没到字符串尾)
{
记录当前字符位置;
while(当前字符是空格)
{
字符入栈;
}
从刚才记录的位置开始字符全部出栈;
}

具体代码自己实现吧~
[解决办法]
用栈实现,先输入,遇到空格就输出栈.

[解决办法]
探讨
试着写了一下,吼吼...

C/C++ code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
int i;
char* src = "this is a test";
char* ptr = src + strlen(src) - 1;
for (i = strlen(src); i > 0; i--)
{
printf("%c", *ptr--);
}
system("pause");
return 0;
}

热点排行