【问题标题】:How can I call function from other function in C如何从 C 中的其他函数调用函数
【发布时间】:2020-12-02 23:19:42
【问题描述】:

我尝试编写 C 程序,用户可以在其中输入字符串,程序应该检查字符串是否为回文。该字符串也可以是诸如“没有柠檬,没有甜瓜”之类的句子。我有一个函数“checkForSpaceAndChar”从句子中删除空格,另一个函数“isPalindrome”检查字符串是否为回文。现在我尝试弄清楚如何首先获取输入的字符串并删除空格和特殊字符,然后检查该字符串是否为回文。

#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;
}

【问题讨论】:

  • 你已经描述了你想要做什么,但你没有描述你对所示代码有什么具体的错误或问题。
  • isPalindrome() 不正确。它总是在循环的第一次迭代期间返回,因此它只检查第一个字符是否与最后一个字符相同。 return 1 应该在循环之后,而不是在 else 中。
  • OT:两次调用isPalidrome 效率低下。只需调用一次并保存返回值。尽管在这种情况下,使用if () { ..} else { .. } 而不是检查返回值两次更为常见。
  • scanf("%s")gets 有相同的问题。永远不要使用getsstackoverflow.com/questions/1694036/… 作为直接推论,永远不要使用scanf("%s"...)
  • 关于:for (int i = 0; i &lt; strlen(stringWithoutSpace); i++) { 函数:strlen() 返回一个size_t,它是一个无符号值。该语句将其与 int 进行比较,这是一个有符号值。

标签: c c-strings palindrome function-definition


【解决方案1】:

checkForSpaceAndChar() 需要使用两个索引变量,一个用于输入数组,另一个用于输出数组。按照您编写的方式,它将所有字母字符复制到newArray,但保持与非字母字符对应的元素不变。所以你会得到未初始化条目的间隙。

它应该是从另一个方向复制,从stringWithoutSpacenewArray

由于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];
        }
    }
}

由于newArray 是它复制到的位置,因此您需要将其用作isPalindrome() 的参数。

        if (isPalindrome(newArray)) {
            printf("This sentence is a palindrome.");
        } else {
            printf("This sentence is not a palindrome.");
        }

【讨论】:

  • checkForSpaceAndChar 不返回值。也许这应该是一个 void func?
【解决方案2】:

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

所以这样的做法不好。

函数isPalindrome 可以写成例如以下方式,而不需要删除非字母字符。

#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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 1970-01-01
    • 2019-09-24
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多