如何从C中的其他函数调用函数

如何从C中的其他函数调用函数,c,c-strings,palindrome,function-definition,C,C Strings,Palindrome,Function Definition,我尝试编写一个C程序,用户可以在其中输入一个字符串,程序应该检查该字符串是否为回文。字符串也可以是“没有柠檬,没有瓜”这样的句子。我有一个函数“checkForSpaceAndChar”用于删除句子中的空格,还有一个函数“isAlindrome”用于检查字符串是否为回文。现在,我试图找出如何首先获取输入的字符串并删除空格和特殊字符,然后检查该字符串是否为回文 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <

我尝试编写一个C程序,用户可以在其中输入一个字符串,程序应该检查该字符串是否为回文。字符串也可以是“没有柠檬,没有瓜”这样的句子。我有一个函数“checkForSpaceAndChar”用于删除句子中的空格,还有一个函数“isAlindrome”用于检查字符串是否为回文。现在,我试图找出如何首先获取输入的字符串并删除空格和特殊字符,然后检查该字符串是否为回文

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
#include <string.h>

int isPalindrome(char inputeString[]){
    int begin = 0, end = strlen(inputeString) - 1;

    while (end > 1) {
        if (inputeString[begin++] != inputeString[end--]) {
            return 0;
        }
        else {
            return 1;
        }
    }
}

char checkForSpaceAndChar(char stringWithoutSpace[], char newArray[]) {

    for (int i = 0; i < strlen(stringWithoutSpace); i++) {
        if (isalpha(stringWithoutSpace[i]) != 0) {
            stringWithoutSpace[i] = newArray[i];
        }
    }
}
#define SIZE 1000

int main(void) {
    int repeat = 1;
    char arrayPalindrome[SIZE], newArray[SIZE];

    while (repeat == 1) {
        printf("Enter a sentence: ");
        scanf("%s", arrayPalindrome);

        checkForSpaceAndChar(arrayPalindrome, newArray);

        if (isPalindrome(arrayPalindrome) == 0) {
            printf("This sentence is not a palindrome.");
        }
        if (isPalindrome(arrayPalindrome) == 1) {
            printf("This sentence is a palindrome.");
        }
        printf("\n\nDo you want to enter another sentence (0 for no, 1 for yes)?");
        scanf_s("%d", &repeat);
    }

    return 0;
}
\define\u CRT\u SECURE\u NO\u警告
#包括
#包括
#包括
int isPalindrome(字符输入限制[]){
int begin=0,end=strlen(输入限制)-1;
而(结束>1){
如果(输入限制[begin++]!=输入限制[end--]){
返回0;
}
否则{
返回1;
}
}
}
char checkForSpaceAndChar(char stringWithoutSpace[],char newArray[]{
for(int i=0;i
checkForSpaceAndChar()
需要使用两个索引变量,一个用于输入数组,另一个用于输出数组。按照您编写的方式,它将所有字母字符复制到
newArray
,但保留与非字母字符对应的元素不变。因此,您可以获得未初始化项的间隙

它应该向另一个方向复制,从
stringWithoutSpace
复制到
newArray

由于
checkForSpaceAndChar
不返回任何内容,因此应将其声明为
void

void checkForSpaceAndChar(char stringWithoutSpace[], char newArray[]) {
    int j = 0;
    for (int i = 0; i < strlen(stringWithoutSpace); i++) {
        if (isalpha(stringWithoutSpace[i]) != 0) {
            newArray[j++] = stringWithoutSpace[i];
        }
    }
}

要检查字符串的字母字符序列是否构成回文,无需删除非字母字符。此外,如果用户将字符串文本作为函数参数传递,那么删除非字母字符的函数将调用未定义的行为

因此,这种做法并不好

函数
isAlindrome
可以通过以下方式编写,无需删除非字母字符

#include <stdio.h>
#include <string.h>
#include <ctype.h>


int isPalindrome( const char *s )
{
    const char *p = s + strlen( s );

    do
    {
        while ( s != p && !isalpha( ( unsigned char )*s ) ) ++s;
        
        while ( s != p && !isalpha( ( unsigned char )*--p ) );
    } while ( s != p && *s == *p && ++s );
    
    return s == p;
}

int main(void) 
{
    const char *s = "a bc, ba!";
    printf( "\"%s\" is palindrome: %s.\n", s, isPalindrome( s ) ? "true" : "false" );

    return 0;
}

您已经描述了要执行的操作,但没有描述所示代码的具体错误或问题。
isAlindrome()
不正确。它总是在循环的第一次迭代期间返回,因此它只检查第一个字符是否与最后一个字符相同
return1
应该在循环之后,而不是在
else
中。OT:调用
isPalidrome
两次效率低下。只需调用一次并保存返回值。虽然在这种情况下,更常见的是使用
if(){..}else{..}
而不是两次检查返回值。
scanf(“%s”)
get
有相同的问题。切勿使用
获取
。作为一个直接推论,永远不要使用
scanf(“%s”…)
关于:
for(int i=0;i
函数:
strlen()
返回一个
size\u t
无符号值。此语句将其与一个
int
有符号值进行比较。checkForSpaceAndChar不返回值。可能这应该是一个void funct?
#include <stdio.h>
#include <string.h>
#include <ctype.h>


int isPalindrome( const char *s )
{
    const char *p = s + strlen( s );

    do
    {
        while ( s != p && !isalpha( ( unsigned char )*s ) ) ++s;
        
        while ( s != p && !isalpha( ( unsigned char )*--p ) );
    } while ( s != p && *s == *p && ++s );
    
    return s == p;
}

int main(void) 
{
    const char *s = "a bc, ba!";
    printf( "\"%s\" is palindrome: %s.\n", s, isPalindrome( s ) ? "true" : "false" );

    return 0;
}
"a bc, ba!" is palindrome: true.