请教个字符串问题

xiamihouwei 2007-04-03 05:04:37
我欲将"I like to go to shool"-------->"shool to go to like I"希望帮忙前辈帮帮忙。
...全文
202 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiamihouwei 2007-04-03
  • 打赏
  • 举报
回复
#include "stdio.h"
#define MAX_N 100
char *fun(char *pString)
{ int cnt,num= 0;
char *temp,*pSpace,*space=" ";
char cBuff[20][16],LastChar;

temp=pString;
LastChar=*(temp+strlen(temp)-1);
while(*temp!='\0')
{
pSpace=strchr(temp ,' ');
if(pSpace!=NULL)
{
strncpy(cBuff[num++],temp, strlen(temp)-strlen(pSpace));
pSpace++;
temp=pSpace;
}
else if(LastChar<'z'&&LastChar>'a'||LastChar<'Z'&&LastChar>'A')
{
strncpy(cBuff[num],temp,strlen(temp));
break;
}
else
{
strncpy(cBuff[num],temp,strlen(temp)-1);
strcpy(cBuff[num+1],temp+strlen(temp)-1);
break;
}
}
memset(pString, 0, strlen(pString));
for(cnt=num;cnt>=0;cnt--)
{
if(cnt!=0)
{
strcat(pString,cBuff[cnt]);
strcat(pString,space);
}
else
{ strcat(pString,cBuff[cnt]);
strcat(pString,cBuff[num+1]);
}
}
return pString;
}
main()
{
char *pString=(char*)malloc(sizeof(char)*MAX_N);
char *achar=NULL;
gets(pString);
achar= fun(pString);
puts(achar);
}
我最后的版本谢谢大家支持!
xiamihouwei 2007-04-03
  • 打赏
  • 举报
回复
谢谢你 请问下用STRTOK怎么省代码。
samio_520 2007-04-03
  • 打赏
  • 举报
回复
不过他的函数里好像有BUG哦
samio_520 2007-04-03
  • 打赏
  • 举报
回复
哦,是有这么个函数strtok,要省掉我好多代码啊,呵呵
samio_520 2007-04-03
  • 打赏
  • 举报
回复
将空格间的子串提取出来放到一个指针数组中,再反向输出指针数组中的内容即可
#include <stdio.h>
#include <string.h>
#define MAX_LEN 256

void RevStr(char * Source)
{
char *SrTemp; //输入参数的备份指针
char *pEnd; //无限循环的中断标志指针
char *cBuff[MAX_LEN]; //提取出来的子串存放地
int cnt = 0; //循环变量
int Num = 0; //记录子串个数

//初始化
for(cnt; cnt < MAX_LEN; cnt++)
{
cBuff[cnt] = NULL;
}

SrTemp = Source;
while(1)
  {
pEnd = strstr(SrTemp, " ");//返回第一次出现空格时的指针pEnd
if(0 == pEnd)
{
break; //没有空格时,结束循环
}

//动态分配指针数组
cBuff[Num] = (char*)malloc(strlen(SrTemp) - strlen(pEnd) + 1);

//提取子串
strncpy(cBuff[Num++], SrTemp, strlen(SrTemp) - strlen(pEnd));
while(' ' == *pEnd)
{
pEnd++; //将当前指针移到非空格的字符前
}

SrTemp = pEnd; //继续循环
}

for(cnt = Num - 1; cnt >= 0; cnt--)
{
//反向输入到Source中
memset(Source, 0, strlen(Source));
strcat(Source, cBuff[cnt]);

//释放指针
free(cBuff[cnt]);
cBuff[cnt] = NULL;
}

return;
}
xiamihouwei 2007-04-03
  • 打赏
  • 举报
回复
strchr也可以替换strtok吧!很感谢你!
xiamihouwei 2007-04-03
  • 打赏
  • 举报
回复
这样做把原来的空格给没了
Mypiger 2007-04-03
  • 打赏
  • 举报
回复

char strTmp[32],strTmp1[8][16],*token = NULL,seps[] = " ";
int j = 0;
strcpy( strTmp, "I like to go to shool" );

token = strtok( strTmp, seps );
while( token != NULL )
{
strcpy( strTmp1[j], token );
token = strtok( NULL, seps );
j++;
}

//显示
for( int i=j-1; i>0; i-- )
printf( "%s ", strTmp1[i]);
getchar();

不好似strTmp越界了!改成32位
Mypiger 2007-04-03
  • 打赏
  • 举报
回复
char strTmp[16],strTmp1[8][16],*token = NULL,seps[] = " ";
int j = 0;
strcpy( strTmp, "I like to go to shool" );

token = strtok( strTmp, seps );
while( token != NULL )
{
strcpy( strTmp1[j], token );
token = strtok( NULL, seps );
j++;
}

//显示
for( int i=j-1; i>0; i-- )
printf( "%s ", strTmp1[i]);
getchar();
return;
hellox 2007-04-03
  • 打赏
  • 举报
回复
别忘了#include<algorithm>
hellox 2007-04-03
  • 打赏
  • 举报
回复
reverse

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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