请问如何把一个字符串数组反转输出

skycncomp 2004-05-04 11:28:16
如数组a[100]={ "we must study C hard.");
输出 .hard C study must we
...全文
551 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
skycncomp 2004-05-06
  • 打赏
  • 举报
回复
cngdzhang()

看了一下你的这个,
为什么长度在增加一倍。r=a+strlen(a)-1;

能说一下实现的方法吗?
lei601 2004-05-06
  • 打赏
  • 举报
回复
#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或者以?结束

上面的人可以试下
eshowjow 2004-05-06
  • 打赏
  • 举报
回复
#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");
}
redguardtoo 2004-05-06
  • 打赏
  • 举报
回复
用简单的自顶向下法分析出解析词和非词,然后反向输出到缓冲区
全部的代码用标准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;
}
languagec 2004-05-06
  • 打赏
  • 举报
回复
指针从字符串末尾开始,若不是字母就输出该字符,并输出从下一个位置开始的字符串直到碰到非字母的字符。
Print() 是从当前指针开始输出字符串,直到碰到非字母为止。
循环直到指针移到开头。
languagec 2004-05-06
  • 打赏
  • 举报
回复
这个能行

#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;
}
buptwaitme2002 2004-05-06
  • 打赏
  • 举报
回复
This is very easy problem.Y can see it as an array,you also can see it as dealing with point!
cngdzhang 2004-05-06
  • 打赏
  • 举报
回复
不是直接翻转,是以词为单位翻转
.................

直接翻转我在1楼就有了.........
babam 2004-05-06
  • 打赏
  • 举报
回复
有现成的为什么要自己写呢?strrev()
cngdzhang 2004-05-06
  • 打赏
  • 举报
回复
a是字符数组的首地址
strlen(a)是字符串a的长度
那么
r=a+strlen(a)-1;
r指向的就是字符串a的最后一个字符了
wdy9927 2004-05-06
  • 打赏
  • 举报
回复
不是长度增加一倍,而是把指针r指向a中的最后一个字符。

cngdzhang 2004-05-04
  • 打赏
  • 举报
回复
上面的还多打了一个空格,

这个好了,


#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;
}
}
}
cngdzhang 2004-05-04
  • 打赏
  • 举报
回复
这个差不多

#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;
}
}
}
junnyfeng 2004-05-04
  • 打赏
  • 举报
回复
给个算法了,剩下的自己搞定

#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++;
}
}

}
skycncomp 2004-05-04
  • 打赏
  • 举报
回复
我写出来的和楼上的一样效果。晕.
谁帮忙看看,写两句。或者给下思路.
我的思路是反相输出,然后检测到空格,借用一个变量再正向输出
反复如此,支到所有反相反输出完毕。
把变量中的字符复给数组。完毕。不过好像太麻烦。
有谁更好的办法.
cngdzhang 2004-05-04
  • 打赏
  • 举报
回复
晕,错了,不好意思:(
cngdzhang 2004-05-04
  • 打赏
  • 举报
回复

#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");
}
weixiaohua 2004-05-04
  • 打赏
  • 举报
回复
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;
}
cngdzhang 2004-05-04
  • 打赏
  • 举报
回复
自己写也行啊

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;
}
cngdzhang 2004-05-04
  • 打赏
  • 举报
回复
#include <ctype.h>
加载更多回复(6)

69,382

社区成员

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

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