倒序输出字符串

匚匚 2009-12-17 09:30:39
假设:
char *buf="I love China" ;
(考虑这些因素:buf=" I love China " 或buf=" I love China "
或buf=" " 或buf="love " 等待)

如何实现倒序输出?(不改变单词。如,China love I,又如: China love I )
如果不是英文而是汉语呢?( char *buf="海上渡船渡上海!" ;
...全文
51136 55 打赏 收藏 转发到动态 举报
写回复
用AI写文章
55 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2012-05-29
  • 打赏
  • 举报
回复
参考楼上的实现了空格个数的正确输出:

//倒序输出英文语句(不改变单词字母的顺序)
#include <iostream>
using std::cout;
using std::endl;

void reprint(char *s) //倒序打印字符
{
char t[32]={},i=0; //把数组设为空,以便在输出时不至于是乱码。 t[32] = {};表示的具体含义不理解?
int count = 0; //用于记录空格数目
while(*s==' '&&*s) //如果指针s指向地址的值为空且存在,则指针s指向地址的下一位
{
s++;
count++;
}

if(*s=='\0') //如果指针s指向字符床的最后一位(结束符'\0'),则结束函数
return;
while(*s!=' ' && *s) //如果指针指针s指向的值不为空且存在,则把指针s指向的值赋给相应的t[i++]
t[i++]=*s++; //把指针s指向地址的值赋给t[i++]后就立即指向下一位
reprint(s); //递归函数
cout << t;
for(int n = 0; n <= count; n++) //打印出相应的空格数
cout << " ";
}
void reprint_c(char *s) //倒序打印汉字
{
char t[8]={},i=0;
int count = 0;
while(*s==' ' && *s)
{
s++;
count++;
}

if(*s=='\0')
return;
while(*s!=' '&&*s)
t[i++]=*s++;
reprint_c(s);
cout << t << " ";
for(int n = 0; n <= count; n++) //打印出相应的空格数
cout << " ";
}
int main()
{
char *str= " Beijing Changping Tiantongyuan !";
char *str2= " 北京市 昌平区 天通苑 ! ";

reprint(str);
cout << endl;
reprint_c(str2);
cout << endl;

system("pause");
return 0;
}

钱国正 2011-09-07
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 bobommsky 的回复:]
C/C++ code


/*递归实现到位输出*/
void UnPrint(void)
{
char c;
c=getchar();
if(c=='\n')
{
return ;
}
UnPrint();
putchar(c);……
[/Quote]昨天笔试就有这题
jackylongchen 2011-04-06
  • 打赏
  • 举报
回复
C/C++

改进了一下

#include <stdio.h>
#define MAX 1000

void reprint(char *s) //打印字符
{
char t[32]={0},i=0;
while(*s==' '&&*s)
s++;
if(*s=='\0')
return;
while(*s!=' '&&*s)
t[i++]=*s++;
reprint(s);
printf("%s ",t);
}
void reprint_c(char *s)//打印汉字
{
char t[8]={0},i=0;
while(*s==' '&&*s)
s++;
if(*s=='\0')
return;
while(*s!=' '&&*s&&i<2)
t[i++]=*s++;
reprint_c(s);
printf("%s",t);
}
int main(void)
{
char str1[MAX],str2[MAX];
printf ("请输入英语句子:\n");
gets(str1);
printf("\n你输入的英文语句是:\n%s\n", str1);
printf("\n转换后的英文语句:\n");
reprint(str1);

printf("\n*************************\n");

printf ("\n请输入中文句子:\n");
gets(str2);
printf("\n你输入的中文语句是:\n%s\n", str2);
printf("\n转换后的中文语句:\n");
reprint_c(str2);
printf("\n\n");
return 0;
}
jackylongchen 2011-04-06
  • 打赏
  • 举报
回复
C/C++

改进了一下

#include <stdio.h>
#define MAX 1000

void reprint(char *s) //打印字符
{
char t[32]={0},i=0;
while(*s==' '&&*s)
s++;
if(*s=='\0')
return;
while(*s!=' '&&*s)
t[i++]=*s++;
reprint(s);
printf("%s ",t);
}
void reprint_c(char *s)//打印汉字
{
char t[8]={0},i=0;
while(*s==' '&&*s)
s++;
if(*s=='\0')
return;
while(*s!=' '&&*s&&i<2)
t[i++]=*s++;
reprint_c(s);
printf("%s",t);
}
int main(void)
{
char str1[MAX],str2[MAX];
printf ("请输入英语句子:\n");
gets(str1);
printf("\n你输入的英文语句是:\n%s\n", str1);
printf("\n转换后的英文语句:\n");
reprint(str1);

printf("\n*************************\n");

printf ("\n请输入中文句子:\n");
gets(str2);
printf("\n你输入的中文语句是:\n%s\n", str2);
printf("\n转换后的中文语句:\n");
reprint_c(str2);
printf("\n\n");
return 0;
}
jackylongchen 2011-04-06
  • 打赏
  • 举报
回复
O(∩_∩)O哈哈~
今天学习了。。
hk_mars 2010-07-16
  • 打赏
  • 举报
回复
1. 先把整个句子按字符倒过来 
2. 用递归方法,把倒过的句子中的每个单词再倒回来

整个下来,不使用另外的内存放最后的句子
bobommsky 2009-12-20
  • 打赏
  • 举报
回复
一二三四三二一
我是人大的!
[Quote=引用 45 楼 huiguixian 的回复:]
引用 6 楼 z569362161 的回复:
    天连水尾水连天!



i服了you 真的是中文系的?
[/Quote]
匚匚 2009-12-20
  • 打赏
  • 举报
回复
十分感谢!不错!
[Quote=引用 48 楼 cracksa 的回复:]
前段时间做的,不知道是不是你要的结果
如:I am a students, so is he. 倒置结果:.he is so ,students a am I
C/C++ code
#include<stdio.h>
#include<string.h>
#include<stdlib.h>#define MAX 1000
main()
{void test(char*,int);int n;char a[MAX];
printf ("请输入英语句子:\n");
gets(a);
printf("\n你输入的英文语句是:\n%s\n", a);
n= strlen(a)-1;
test(a, n);
printf("转换后的英文语句:\n%s\n", a);
system("pause");
}void test(char*p,int n)
{int a, b, d, c=0;char ch[MAX];for (a= n, b= n; a>=0; a--)
{if (32<= p[a]&& p[a]<=47)
{for (d= a+1; d< b; d++)
{
ch[c++]= p[d];
}
ch[c++]= p[a];
b= a;
}if (a==0)
{for (d= a; d< b; d++)
{
ch[c++]= p[d];
}
}
}
ch[c]='\0';
strcpy(p, ch);
}
[/Quote]
cracksa 2009-12-20
  • 打赏
  • 举报
回复
前段时间做的,不知道是不是你要的结果
如:I am a students, so is he. 倒置结果:.he is so ,students a am I

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 1000
main()
{
void test(char*, int);
int n;
char a[MAX];
printf ("请输入英语句子:\n");
gets(a);
printf("\n你输入的英文语句是:\n%s\n", a);
n = strlen(a) - 1;
test(a, n);
printf("转换后的英文语句:\n%s\n", a);
system("pause");
}

void test(char *p, int n)
{
int a, b, d, c = 0;
char ch[MAX];
for (a = n, b = n; a >= 0; a--)
{
if (32 <= p[a] && p[a] <= 47)
{
for (d = a +1; d < b; d++)
{
ch[c++] = p[d];
}
ch[c++] = p[a];
b = a;
}
if (a == 0)
{
for (d = a; d < b; d++)
{
ch[c++] = p[d];
}
}
}
ch[c] = '\0';
strcpy(p, ch);
}
匚匚 2009-12-20
  • 打赏
  • 举报
回复
[期待你的大作
Quote=引用 46 楼 areegod 的回复:]
要写的话 只要会C的都能写出来 主要还是看 代码 和效率 
[/Quote]
areegod 2009-12-20
  • 打赏
  • 举报
回复
要写的话 只要会C的都能写出来 主要还是看 代码 和效率
小小攻城师 2009-12-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 z569362161 的回复:]
    天连水尾水连天!


[/Quote]
i服了you 真的是中文系的?
匚匚 2009-12-19
  • 打赏
  • 举报
回复
期待你的代码......
[Quote=引用 23 楼 zsm180 的回复:]
C/C++ code
建立一个一维线性数组,遍历一遍字符串,遇到空格记录偏移量,然后根据偏移数组和字符串首地址定位既可以按要求倒序输出:" I Love China !"int space_array[]= {0,1,3,8,14,15,17,18,19};//19是'\0'偏移量打印倒数第一个空格:19-18=1//空格至'\0'间无数据,打印空格18-17=1//空格之间无数据,打印空格1?-
[/Quote]
yuiyuityuiyui 2009-12-19
  • 打赏
  • 举报
回复
不错。。。
匚匚 2009-12-19
  • 打赏
  • 举报
回复
期待更精妙的算法
匚匚 2009-12-19
  • 打赏
  • 举报
回复
果然有货!对库函数掌握得不少!但风格不怎么样,呵呵
我还是觉得29楼的代码虽然冗长,效率也不知道怎么样?但完全能满足我的要求,请大家进行简化?递归?
[Quote=引用 40 楼 hlyces 的回复:]

一个强人的代码,不是我写的,只能英文
[/Quote]
hlyces 2009-12-19
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
char words[50][256],symbols[50],dest[256];

int main()
{
char *str="how@are#you?";
char *src=str,*des=dest;
int i=0,widx=0,widx2=0,sidx=0;
for(;*src!='\0'; isalpha(*src) ? (words[widx][widx2++]=*src++) :(widx++,widx2=0,symbols[sidx++]=*src++));
for(i=0;i<widx;i++) for(src=words[widx-i-1];*src!='\0' || (*des++=symbols[i],0);*des++=*src++);
puts(dest);
return 0;
};

一个强人的代码,不是我写的,只能英文
匚匚 2009-12-19
  • 打赏
  • 举报
回复
十分感谢!
不过,好像还是不能正确或全部地输出空格。
另,你的输出汉字函数,当汉字字符串间无空格时,是如何做到按原序输出的,按我对递归及堆栈的理解,应该是倒序才对啊?
[Quote=引用 38 楼 selooloo 的回复:]
改下就能打印空格了,中英文混排的形式我觉得无法实现,因为一个汉字占两字节,无法把一个汉字和两个挨着的字母区分开,如果是汉字里插入一个字母,就会造成整个汉字的乱码。
C/C++ codevoid reprint(char*s)//打印字符{char t[32]={},i=0,t1[32]={};while(*s==''&&*s)
t1[i++]=*s++;
i=0;if(*s=='\0')return;while(*s!=''&&*s)
t[i++]=*s++;
reprint(s);
printf("%s%s",t1,t);
}
[/Quote]
selooloo 2009-12-18
  • 打赏
  • 举报
回复
[Quote=引用 32 楼 zhw952 的回复:]
十分强劲!
另:
    即使是最前面的空格也要打印出;
    如果是中英文混排呢?
    希望能得到你的指点
[/Quote]

改下就能打印空格了,中英文混排的形式我觉得无法实现,因为一个汉字占两字节,无法把一个汉字和两个挨着的字母区分开,如果是汉字里插入一个字母,就会造成整个汉字的乱码。

void reprint(char *s)//打印字符
{
char t[32]={},i=0,t1[32]={};
while(*s==' '&&*s)
t1[i++]=*s++;
i=0;
if(*s=='\0') return;
while(*s!=' '&&*s)
t[i++]=*s++;
reprint(s);
printf("%s%s",t1,t);
}
ld6886 2009-12-18
  • 打赏
  • 举报
回复
STL中有的是现成的方法
加载更多回复(35)

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧