请问如何把一个字符串数组反转输出
如数组a[100]={ "we must study C hard.");
输出 .hard C study must we
问题点数:20、回复次数:26Top
1 楼cngdzhang()回复于 2004-05-04 11:50:52 得分 1
#include <stdio.h>
void main()
{
char a[100]="we must study C hard.";
char *p=a;
while(*p) p++;
while(p>=a)
{
printf("%c",*p--);
}
printf("\n");
}
Top
2 楼cngdzhang()回复于 2004-05-04 11:51:46 得分 0
晕,错了,不好意思:(
Top
3 楼skycncomp(闭关修练到年底)回复于 2004-05-04 11:58:04 得分 0
我写出来的和楼上的一样效果。晕.
谁帮忙看看,写两句。或者给下思路.
我的思路是反相输出,然后检测到空格,借用一个变量再正向输出
反复如此,支到所有反相反输出完毕。
把变量中的字符复给数组。完毕。不过好像太麻烦。
有谁更好的办法.Top
4 楼junnyfeng(风歌)回复于 2004-05-04 12:03:01 得分 1
给个算法了,剩下的自己搞定
#include <stdio.h>
#include <string.h>
void main()
{
char a[100]="we must study C hard.";
char b[20];
int i,j=0;
i=strlen(a)-1;
for(;i>=0;i--)
{
if(a[i]!=' ')
b[j++]=a[i];
if(a[i]==' ' || i==0)
{
while(j+1)
printf("%c",b[--j]);
j++;
}
}
}Top
5 楼cngdzhang()回复于 2004-05-04 12:15:04 得分 1
这个差不多
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void main()
{
char a[100]="we must study C hard.";
char *p,*q,*r;
r=a+strlen(a);
p=r-1;
while(p>=a)
{
if(!isalnum(*p))
{
while(!isalnum(*p) && p>=a) p--;
q=p;
while(q!=r)
{
q++;
printf("%c",*q);
}
r=p;
}
if(isalnum(*p))
{
while(isalnum(*p) && p>=a) p--;
q=p;
while(q!=r)
{
q++;
printf("%c",*q);
}
r=p;
}
}
}
Top
6 楼cngdzhang()回复于 2004-05-04 12:27:24 得分 1
上面的还多打了一个空格,
这个好了,
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void main()
{
char a[100]="we must study C hard.";
char *p,*q,*r;
r=a+strlen(a)-1;
p=r;
while(p>=a)
{
if(!isalnum(*p))
{
while(!isalnum(*p) && p>=a) p--;
q=p;
while(q!=r)
{
q++;
printf("%c",*q);
}
r=p;
}
if(isalnum(*p))
{
while(isalnum(*p) && p>=a) p--;
q=p;
while(q!=r)
{
q++;
printf("%c",*q);
}
r=p;
}
}
}
Top
7 楼skycncomp(闭关修练到年底)回复于 2004-05-04 15:14:51 得分 0
isalnum这人函数是什么意思?起什么作用。
不太明白,望解释一下。Top
8 楼cngdzhang()回复于 2004-05-04 15:29:54 得分 2
isalnum函数
判断一个字符是不是'a'-'z' 'A'-'Z' 和 '0'-'9'
isdigit
判断'0'-'9';
isalpha
判断 'a'-'z' 'A'-'Z'
Top
9 楼mayabuluo(玛雅部落)回复于 2004-05-04 15:38:28 得分 0
倒了!有没有更简单的方法?Top
10 楼ipgk(loboho)回复于 2004-05-04 15:52:25 得分 2
/*
用库函数
char *strrev(char *s)
将字符串s中的字符全部颠倒顺序重新排列,并返回排列后的字符串
*/
#include <string.h>
#include <stdio.h>
main()
{
char a[100]="we must study C hard.";
strrev(a);
puts(a);
}Top
11 楼skycncomp(闭关修练到年底)回复于 2004-05-04 16:57:16 得分 0
isalnum函数
这个函数是在哪个头文件里面。Top
12 楼skycncomp(闭关修练到年底)回复于 2004-05-04 16:57:57 得分 0
是如来定义的。Top
13 楼cngdzhang()回复于 2004-05-04 16:58:38 得分 2
#include <ctype.h>
Top
14 楼cngdzhang()回复于 2004-05-04 17:00:47 得分 2
自己写也行啊
int MyIsalnum(char c)
{
if(c>='a' && c<='z') return 1;
if(c>='A' && c<='Z') return 1;
if(c>='0' && c<='9') return 1;
return 0;
}Top
15 楼weixiaohua(我爱Delphi)回复于 2004-05-04 18:28:19 得分 2
int main()
{
char a[100] = {"we111 must study C hard."};
int pos, ipos = strlen(a);
for(int ix=strlen(a); ix>=0; ix--)
if(a[ix] == ' ')
{
pos = ix;
while(pos++ < ipos)
cout << a[pos];
ipos = ix;
}
else if(ix == 0)
{
pos = ix;
while(pos<= ipos)
{
cout << a[pos];
pos++;
}
}
cout << endl;
system("PAUSE");
return 0;
}Top
16 楼skycncomp(闭关修练到年底)回复于 2004-05-06 12:23:27 得分 0
cngdzhang()
看了一下你的这个,
为什么长度在增加一倍。r=a+strlen(a)-1;
能说一下实现的方法吗?
Top
17 楼wdy9927()回复于 2004-05-06 15:25:28 得分 1
不是长度增加一倍,而是把指针r指向a中的最后一个字符。
Top
18 楼cngdzhang()回复于 2004-05-06 15:41:02 得分 1
a是字符数组的首地址
strlen(a)是字符串a的长度
那么
r=a+strlen(a)-1;
r指向的就是字符串a的最后一个字符了
Top
19 楼babam()回复于 2004-05-06 15:50:56 得分 0
有现成的为什么要自己写呢?strrev()Top
20 楼cngdzhang()回复于 2004-05-06 15:55:10 得分 0
不是直接翻转,是以词为单位翻转
.................
直接翻转我在1楼就有了.........
Top
21 楼buptwaitme2002(大漠孤鹰)回复于 2004-05-06 16:19:07 得分 0
This is very easy problem.Y can see it as an array,you also can see it as dealing with point!Top
22 楼languagec(各有所求)回复于 2004-05-06 16:40:16 得分 0
这个能行
#include <iostream.h>
void Print(char *p)
{
while((*p>='a'&&*p<='z')||(*p>='A'&&*p<='Z'))
{
cout<<*p;
p++;
}
}
int main()
{
char arry[]="we must study C hard.";
char *p=arry;
while(*p) p++;
p--;
while(p>=arry)
{
if(!((*p>='a'&&*p<='z')||(*p>='A'&&*p<='Z')))
{
Print(p+1);
cout<<*p;
}
p--;
}
Print(arry);
cout<<endl;
return 0;
}Top
23 楼languagec(各有所求)回复于 2004-05-06 16:44:34 得分 0
指针从字符串末尾开始,若不是字母就输出该字符,并输出从下一个位置开始的字符串直到碰到非字母的字符。
Print() 是从当前指针开始输出字符串,直到碰到非字母为止。
循环直到指针移到开头。
Top
24 楼redguardtoo()回复于 2004-05-06 17:29:24 得分 1
用简单的自顶向下法分析出解析词和非词,然后反向输出到缓冲区
全部的代码用标准C写成(包括test case),环境为VC6,win2000
#include <assert.h>
#include <string.h>
/*-------------------declaration-------------------*/
int
is_wordchr(char c);
int
reverse_str(const char* in_str,
int in_str_len,
char* out_buf,
int out_buf_size,
int (*is_word_char)(char )
);
static int dump_last_token(const char* in_str,
int in_str_len,
char* out_buf,
int (*is_word_char)(char ));
/*-------------------definition-------------------*/
int
/*============================================================*Author :redguardtoo
Date :2004-5-6
Description: reverse in_str and output the result int out_buf
Return :int (by default) - 0, failed; 1, succeed
Parameters :
const char* in_str -
int in_str_len -
char* out_buf -
int out_buf_size -
int (*is_word_char(char) - callback, if a char is word charater
Note :
\*============================================================*/
reverse_str(const char* in_str,
int in_str_len,
char* out_buf,
int out_buf_size,
int (*is_word_char)(char )
)
{
int ret;
assert(in_str);
assert(in_str_len>0);
assert(out_buf);
assert(out_buf_size>0);
assert(is_word_char);
assert(in_str_len<=out_buf_size);
if(!in_str_len||!out_buf_size)
goto errorhandle1;
ret=0;
do
{
ret=dump_last_token(in_str,in_str_len,out_buf,is_word_char);
in_str_len-=ret;
out_buf+=ret;
}while(ret);
return 1;
errorhandle1:
return 0;
}
/*============================================================*Author :redguardtoo
Date :2004-5-6
Description: A token is a sequence of word chararcters
or a sequence of non-word characers
Return :int - 0, failed; >0, the length of the dumpped token
Parameters :
const char* in_str -
int in_str_len -
char* out_buf -
int (*is_word_char) -
Note :
\*============================================================*/
static int
dump_last_token(const char* in_str,
int in_str_len,
char* out_buf,
int (*is_word_char)(char ))
{
int cnt;
int rlt;
if(in_str_len<=0)
goto errorhandle1;
for(cnt=0,rlt=is_word_char(in_str[in_str_len-1]);
rlt==is_word_char(in_str[in_str_len-1])
&& in_str_len>0;
in_str_len--,cnt++
)
{
;
}
strncpy(out_buf,in_str+in_str_len,cnt);
out_buf[cnt]=0;
return cnt;
errorhandle1:
return 0;
}
/*============================================================*Author :redguard
Date :2004-5-6
Description: if a character is a word character
Return :int -
Parameters :
char c -
Note :
\*============================================================*/
int is_wordchr(char c)
{
if(c>='a'&&c<='z')
return 1;
if(c>='A'&&c<='Z')
return 1;
if(c=='_')
return 1;
if(c>='0'&&c<='9')
return 1;
return 0;
}
/*-------------------test main routine-------------------*/
int main(int argc, char* argv[])
{
#if 1
{
char str[]="abcdef 1234";
char buf[255];
int ret;
ret=dump_last_token(str,strlen(str),buf,is_wordchr);
assert(ret==4);
assert(strcmp(buf,"1234")==0);
}/*test case 1*/
#endif
#if 1
{
char str[]="abcdef ";
char buf[255];
int ret;
ret=dump_last_token(str,strlen(str),buf,is_wordchr);
assert(ret==3);
assert(strcmp(buf," ")==0);
}/*test case 2*/
#endif
#if 1
{
char str[]="def#hi_jk ##( 1823";
char buf[255];
int ret;
ret=reverse_str(str,strlen(str),
buf,sizeof(buf)/sizeof(buf[0]),
is_wordchr);
assert(ret);
assert(strcmp(buf,"1823 ##( hi_jk#def")==0);
}/*test case 3*/
#endif
return 0;
}
Top
25 楼eshowjow(Ж※★※Ж)回复于 2004-05-06 18:30:23 得分 1
#include "stdio.h"
void main()
{char string[100];
char a[10][20]={0};
int i,n,num=0,word =0;
char c;
gets(string);
for(i=0;(c=string[i])!='\0';i++)
{if(c==' ')word=0;
else if (word==0)
{word =1;
num++;
n=0;
a[num][n]=c;}
else{n++;a[num][n]=c; }
}
for (;num>=0;num--)
printf("%s ",a[num]);
printf("\n");
}Top
26 楼lei601(菜的一塌糊涂)回复于 2004-05-06 20:46:02 得分 2
#include<stdio.h>
void f3()
{static int i=0;
char c;
c=getchar();
i++;
if(c=='?'||i>15)
return;
else f3();
putchar(c);
}
main()
{printf("Input a string:");
f3();
}
上面这个可以实现 输入的字符小于15或者以?结束
上面的人可以试下Top




