[英]strcmp(string,string) for palindrome checker
作为大学入门级软件开发的编程任务,我们的任务是制作一个程序:
strcmp
理解有问题,或者我不完全理解我的代码是做什么来操作字符串的。
测试的输入和输出示例。
输入:
bob
输出:
The word contains 3 letters The word is not a palindrome The word reversed is 'bob'
这里的代码应该输出bob
IS 一个回文,表明比较没有正确运行。
输入:
fish
输出:
The word contains 4 letters The word is not a palindrome The word reversed is 'hsif'
我的代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> //Reverses a string, Takes the string, a pointer to allocated memory and the length of the substring to be reversed void stringReverse(const char s[], char *mem, int n) { char b[n + 1]; b[n + 1] = 0; for (int i = 0; i < n; i++) { b[i] = s[n - (i + 1)]; } strncpy(mem, b, n); } //Checks if 's' is a palindrome bool isPal(char s[], char *c) { stringReverse(s, c, (int)strlen(s) + 1); //printf("%s", s); //used for debugging return !strcmp(c, s); } int main() { char word[100]; gets(word); //This loop used alone works. Calling strcmp(word,rev) in place of isPal(word,c) as well as replacing c with rev in line 38. // char rev[(int)strlen(word)+1]; // for(int i = 0; i<=(int)strlen(word);i++){ // if(i==(int)strlen(word)){rev[i]=0;continue;} // rev[i]=word[(int)strlen(word)-(i+1)]; // } char *c = malloc(strlen(word) + 1); printf("The word contains %d letters\\n", (int)strlen(word)); printf("The word is%sa palindrome\\n", isPal(word, c) ? "" : " not"); stringReverse(word, c, (int)strlen(word)); printf("The word reversed is '%s'\\n", c); free(c); return 0; }
当您的输出正确打印反转字符串时,您可以假设函数stringReverse()
正常工作。 在您的代码中,您对该函数进行了两次调用:
在main()
,打印结果是正确的:
stringReverse(word,c,(int)strlen(word));
但是在isPal()
你有问题:
stringReverse(s,c, (int)strlen(s)+1);
从后一个调用中删除+1
,你应该没问题:
stringReverse(s,c, (int)strlen(s));
然而,正如其他人所提到的,可能有更好的方法来解决这个问题。
您的代码中有多个问题:
b[n + 1] = 0;
在数组末尾之外存储 0
strncpy(mem, b, n);
不为空终止目标数组。 不要使用strncpy
,它的语义令人困惑且容易出错。 由于malloc()
返回的块未初始化,偏移b[n]
处的字节不一定为 0,因此导致strcmp()
返回非 0。
stringReverse(s, c, (int)strlen(s) + 1);
使用错误的字节数值,您甚至不应该传递字节数,只需在stringReverse
使用strlen(s)
stringReverse
。
gets()
已弃用且不安全。 它已从 C 标准的最新版本中删除。 不要使用gets()
,而是使用fgets()
或scanf()
。
请注意,反转后的字符串可以直接存储到目标数组中。
这是一个修改后的版本:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
// Reverse a string:
// takes a pointer to a source string and a pointer to a destination array.
// the destination array must be long enough.
// the source and destination array must not overlap.
// returns a pointer to the destination array (like strcpy)
char *stringReverse(char *dest, const char *src) {
size_t len = strlen(src);
for (size_t i = 0; i < len; i++) {
dest[i] = src[len - (i + 1)];
}
dest[len] = '\0';
return dest;
}
//Checks if 's' is a palindrome
bool isPal(const char *s) {
char tmp[strlen(s) + 1];
stringReverse(tmp, s);
//printf("%s", s); //used for debugging
return !strcmp(tmp, s);
}
int main() {
char word[100];
if (scanf("%99[^\n]", word) != 1)
return 1;
int len = strlen(word);
printf("The word contains %d letters\n", len);
char *p = malloc(len + 1);
if (p != NULL) {
printf("The word is%s a palindrome\n", isPal(word) ? "" : " not");
printf("The word reversed is '%s'\n", stringReverse(p, word));
free(p);
}
return 0;
}
输入: bob
输出:
The word contains 3 letters
The word is a palindrome
The word reversed is 'bob'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.