#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define FALSE 0
#define TRUE 1
int alphabetic(char *string )
{
int i, valid;
valid = TRUE;
for ( i = 0; i < strlen(string); i++ )
{
if ( toupper ( string[i] ) < 'A' || toupper (string[i] ) > 'Z' )
valid = FALSE;
}
return valid;
}
int main()
{
char c, inputarray[10], temp[10];
int i = 0;
strcpy(temp, inputarray);
printf("%s Please enter string>");
while ( ( c = getchar () ) != '\n')
{
if ( i < 9 )
inputarray[i] = c;
i++;
}
if ( i < 10 )
inputarray[i] = '\0';
else
{
inputarray[9] = '\0';
printf("String too long\n");
return;
}
printf("%s\n",inputarray);
if (! alphabetic (inputarray) )
{
printf("Invalid input");
}
if (strcmp(strrev(inputarray),temp) == 0 )
printf("Palindrome\n");
else
printf("Not palindrome\n");
}
尝试这个并且当输入是回文时仍然得到'不是回文'。当我运行程序时,它表示'堆叠在inputarray周围已损坏'。关于如何修复它的任何想法都会读取回文并停止输入数组被破坏。
答案 0 :(得分:1)
这是一种可能的实现方式。我尽可能多地尝试在评论中解释,但如果有些事情不清楚,请放心评论。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
int alphabetic(char *string)
{
int i, valid;
valid = true;
for (i = 0; i < strlen(string); i++)
{
if (toupper(string[i]) < 'A' || toupper(string[i]) > 'Z')
{
valid = false;
// break here we are done;
break;
}
}
return valid;
}
void printArray(char* str)
{
printf("Array = ");
for (int i = 0; i < strlen(str); ++i)
{
printf("%c", str[i]);
}
printf("\n");
}
bool isPalindrome(char* str1, char* str2)
{
bool isValidPalindrome = true;
int length = strlen(str1);
if (length != strlen(str2))
{
printf("Strings must be the same lenth");
isValidPalindrome = false;
}
else
{
--length;
for (int i = length; i >= 0; --i)
{
if (str1[i] != str2[length - i])
{
isValidPalindrome = false;
break;
}
}
}
return isPalindrome;
}
int main()
{
const int length = 10;
char c, inputarray[length], temp[length];
int i = 0;
// Comparing strings that have not been initialized
// produces undefined behavior. Imagine inputArray is equal to:
// inputArray: "my String ... some other unknown stuff"... where does
// the string ends? there is no '\n' in the horizon.
// The stack error you are getting is produced by the statement
// below. I've pusehd this statement below right after inputArray
// has been initialized
// strcpy(temp, inputarray);
// You don't need the format specifier %s unless you
// rewrite your printf statement as printf("%s", "Please enter string");
// for simplicity you can write it as follows
printf("Please enter string: ");
while ((c = getchar()) != '\n')
{
if (i < length - 1)
inputarray[i] = c;
i++;
}
// Pulled the code inside the if to avoid multiple returns // just preference... not needed
if (i < length)
{
inputarray[i] = '\0';
// helper function to print array
printArray(inputarray);
if (!alphabetic(inputarray))
{
printf("Invalid input");
}
// copy the strings here
strcpy(temp, inputarray);
// reverse the string here
strrev(inputarray);
// you will have to roll out your own isPalindrome
// implementation since reversing a string and comparing it
// with itself will always return false e.g.
// inputArray = "hello";
// copy inputArray into temp
// temp = "hello";
// reverse inputArray
// compare strings: "olleh" == "hello" -> false
if (isPalindrome(inputarray, temp) == true)
printf("Palindrome\n");
else
printf("Not palindrome\n");
}
else
{
inputarray[9] = '\0';
printf("String too long\n");
}
return 0;
}
答案 1 :(得分:0)
尝试这个并且当输入是回文时仍然得到'不是回文'。当我运行程序时,它表示“堆叠在inputarray周围已损坏”。
两者的可能原因是在输入strcpy(temp, inputarray)
之前调用inputarray
。在if (strcmp(strrev(inputarray),temp) == 0 )
之前立即移动它,您的程序可能会有效。另一个错误是%s
中的printf("%s Please enter string>")
。