编写代码,以检查是否s2
是只使用一个呼叫isSubString
的s1
转动(即waterbottle
是erbottlewat
旋转)。旋转的字符串
我写这个程序,但我无法得到所需的输出。请指导我哪里出错。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int isRotated(char *s1, char *s2);
int isSubstring(char *s1, char *s2);
int isRotated(char *s1, char *s2)
{
int r;
char s[100];
if(strlen(s1) == strlen(s2))
strcpy(s, s1);
r = isSubstring(s, s2);
strcat(s, s1);
return r;
}
int isSubstring(char *s1, char *s2){
if(strstr(s1, s2))
return 1;
else
return 0;
}
int main(void) {
char s1[100], s2[100];
printf("Enter the first String\n");
scanf("%s", s1);
printf("Enter the second String\n");
scanf("%s", s2);
if(isRotated(s1, s2)==1)
printf("%s and %s are rotated string\n", s1, s2);
else
printf("%s and %s are not rotated string\n", s1, s2);
return 0;
}
什么是STrings' ????? –
“你期望的输出”究竟是什么意思?你得到了什么?错误消息? –
如果两个字符串有不同的长度,'s'是未初始化的,这意味着'isSubString'和'strcat'可能在未正确终止的字符串上运行。 –