C 句子反转功能
包括stdio.h 包括stdlib.h 包括string.hC 句子反转功能,c,string,C,String,包括stdio.h 包括stdlib.h 包括string.h #include <conio.h> #define SIZE 40 int main( void ) { char str[ SIZE ]; char strrev[ SIZE ]; char *temp1; char *temp2; int turn = 0; printf( "Enter line of text: " ); fgets( str, SI
#include <conio.h>
#define SIZE 40
int main( void )
{
char str[ SIZE ];
char strrev[ SIZE ];
char *temp1;
char *temp2;
int turn = 0;
printf( "Enter line of text: " );
fgets( str, SIZE - 1, stdin );
temp1 = strtok( str, " " );
temp2 = strtok( NULL, " " );
strcat( strrev, temp2 );
strcat( strrev, temp1 );
既然最后字符串
temp1
或temp2
中的任何一个都将得到NULL
为什么循环不能正确运行?您可以使用递归反转句子,并使用sscanf拆分字符串:
#include <stdio.h>
void recursive(const char *s)
{
char b[100];
int n;
if (sscanf(s, "%99s%n", b, &n) == 1)
recursive(s + n), printf(" %s", b);
}
int main()
{
recursive("foo bar baz");
return 0;
}
#包括
无效递归(常量字符*s)
{
charb[100];
int n;
如果(sscanf(s,“%99s%n”,b,&n)==1)
递归(s+n),printf(“%s”,b);
}
int main()
{
递归(“foo-bar-baz”);
返回0;
}
我不会使用strtok
,因为1)它会破坏原始字符串,您不能将其与字符串文字一起使用;2)字符串中单词之间的空格数将被打断
我将使用以下方法
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
char *s = "This is a dog";
size_t n = strlen( s );
char t[n + 1];
char *p = t;
char *q = s + n;
while ( q != s )
{
while ( q != s && isblank( *( q - 1 ) ) ) *p++ = *--q;
char *tmp = q;
while ( tmp != s && !isblank( *( tmp - 1 ) ) ) --tmp;
memcpy( p, tmp, q - tmp );
p += q - tmp;
q = tmp;
}
*p = '\0';
puts( s );
puts( t );
return 0;
}
谢谢你的帮助,我想这样解决它:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define SIZE 40
void reverse( char sentence[], char *token2 );
int main( void )
{
char sentence[ SIZE ] = "This is a dog";
char *token1;
char *token2;
token1 = strtok( sentence, " " );
reverse( sentence, token2 );
printf( "%s", token1 );
getche();
return 0;
}//end function main
void reverse( char sentence[], char *token2 )
{
if( token2 == NULL )
return;
else{
token2 = strtok( NULL, " " );
reverse( sentence, token2 );
if( token2 != NULL ){
printf( "%s ", token2 );
}//end if
}//end else
}//end function reverse
#包括
#包括
#包括
#定义尺寸40
无效反向(字符句子[],字符*标记2);
内部主(空)
{
char句子[SIZE]=“这是一只狗”;
字符*1;
字符*2;
token1=strtok(句子“”);
反面(句子,记号2);
printf(“%s”,标记1);
getche();
返回0;
}//端功能主
无效反向(字符句子[],字符*标记2)
{
if(token2==NULL)
返回;
否则{
token2=strtok(NULL,“”);
反面(句子,记号2);
如果(token2!=NULL){
printf(“%s”,标记2);
}//如果结束
}//结束其他
}//端功能反转
当我运行它时,我的程序崩溃了,我对它进行了测试,因此我发现问题在我设置的条件内,而您对每个令牌执行两次strcat测试。您的目标缓冲区可能溢出。另外,要使strcat正常工作,必须以以null结尾的字符串开头。在你的代码中,strev没有初始化。我用了两次strcat,因为我想把这样的句子“thisadog”改成“dogaisthis”,我不认为你的程序可以反转句子。您可以将令牌推到堆栈上,然后按相反顺序打印它们。
This is a dog
dog a is This
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define SIZE 40
void reverse( char sentence[], char *token2 );
int main( void )
{
char sentence[ SIZE ] = "This is a dog";
char *token1;
char *token2;
token1 = strtok( sentence, " " );
reverse( sentence, token2 );
printf( "%s", token1 );
getche();
return 0;
}//end function main
void reverse( char sentence[], char *token2 )
{
if( token2 == NULL )
return;
else{
token2 = strtok( NULL, " " );
reverse( sentence, token2 );
if( token2 != NULL ){
printf( "%s ", token2 );
}//end if
}//end else
}//end function reverse