运行以下C代码后什么都没有打印,什么也不会输出

发布于 2025-01-24 08:47:38 字数 747 浏览 3 评论 0原文

运行以下C代码后,什么都没有打印,什么也不会输出,我不了解发生了什么,请提供帮助。

此代码检查字符串是否为palindrome。

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

char *words[] = {"rar", "tokyo", "level", "prefix", "mom", "delhi", "naruto"};
int size = sizeof(words) / sizeof(words[0]);

void palindChecker(char **, int);

int main() {
    palindChecker(words, size);

    return 0;
}

void palindChecker(char **array, int len) {
    char tempStr[10];
    for (int i = 0; i < len; i++) {
        strcpy(tempStr, strrev(*(array + i)));
        if (strcmp(strrev(*(array + i)), tempStr) == 0) {
            printf("%s is palindrome\n", *(array + i));
        }
        else {
            printf("%s is not palindrome\n", *(array + i));
        }
    }
}

nothing is printing after running the below C code , nothing gets outputted, I am not getting understand what is happening, please help.

This code checks if the string is palindrome or not.

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

char *words[] = {"rar", "tokyo", "level", "prefix", "mom", "delhi", "naruto"};
int size = sizeof(words) / sizeof(words[0]);

void palindChecker(char **, int);

int main() {
    palindChecker(words, size);

    return 0;
}

void palindChecker(char **array, int len) {
    char tempStr[10];
    for (int i = 0; i < len; i++) {
        strcpy(tempStr, strrev(*(array + i)));
        if (strcmp(strrev(*(array + i)), tempStr) == 0) {
            printf("%s is palindrome\n", *(array + i));
        }
        else {
            printf("%s is not palindrome\n", *(array + i));
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人间不值得 2025-01-31 08:47:38

问题:

  • strrev对其提供的字符串进行更改,这意味着您正在更改字符串文字,这是不允许的。您的程序可能会崩溃或做其他意外的事情。

建议:

  • 不要试图更改阵列中的字符串。而是将更改为tempstr
  • 使用const类型声明数组以表明您不允许更改字符串:
      const char *words [] = {“ rar”,“ tokyo”,“ level”,“ prefix”,
                           “妈妈”,“德里”,“火影忍者”};
     

示例:

void palindChecker(const char *array[], int len) {
    for (int i = 0; i < len; i++) {
        char tempStr[strlen(array[i]) + 1];        // a VLA (optional compiler support)
        memcpy(tempStr, array[i], sizeof tempStr); // copy the original string

        if (strcmp(array[i], strrev(tempStr)) == 0) { // reverse `tempStr`
            printf("%s is palindrome\n", array[i]);
        } else {
            printf("%s is not palindrome\n", array[i]);
        }
    }
}

如果您的编译器不支持VLA:S(可变长度阵列),则可以选择适当的内存:

void palindChecker(const char *array[], int len) {
    for (int i = 0; i < len; i++) {
        size_t len = strlen(array[i]) + 1;

        char *tempStr = malloc(len); // allocate space for the string + null terminator
        if(tempStr == NULL) {        // malloc failed
            perror("malloc");
            exit(1);
        }
        memcpy(tempStr, array[i], len);

        if (strcmp(array[i], strrev(tempStr)) == 0) {
            printf("%s is palindrome\n", array[i]);
        } else {
            printf("%s is not palindrome\n", array[i]);
        }

        free(tempStr);               // free the allocated memory
    }
}

或者,请勿完全反转字符串。通过将开始的开始与末端进行比较,然后“走向中间”,请检查字符串是否是回文。

#include <stdbool.h>

bool is_palindrome(const char* str) {
    for (const char *b = str, *e = b + strlen(str); b < e--; ++b) {
        if(*b != *e) return false;
    }
    return true;
}

void palindChecker(const char *array[], int len) {
    for (int i = 0; i < len; i++) {
        if (is_palindrome(array[i])) {
            printf("%s is palindrome\n", array[i]);
        } else {
            printf("%s is not palindrome\n", array[i]);
        }        
    }
}

The issue:

  • strrev makes changes to the string you supply to it, which means that you are making changes to string literals, which is not allowed. Your program may crash or do something else unexpected.

Suggestions:

  • Don't try to make changes to the strings in the array. Instead, make the changes to tempStr.
  • Declare the array using a const type to make it clear that you are not allowed to change the strings:
    const char *words[] = {"rar", "tokyo", "level", "prefix",
                           "mom", "delhi", "naruto"};
    

Example:

void palindChecker(const char *array[], int len) {
    for (int i = 0; i < len; i++) {
        char tempStr[strlen(array[i]) + 1];        // a VLA (optional compiler support)
        memcpy(tempStr, array[i], sizeof tempStr); // copy the original string

        if (strcmp(array[i], strrev(tempStr)) == 0) { // reverse `tempStr`
            printf("%s is palindrome\n", array[i]);
        } else {
            printf("%s is not palindrome\n", array[i]);
        }
    }
}

If your compiler does not support VLA:s (variable length arrays), mallocing the appropriate memory could be an option:

void palindChecker(const char *array[], int len) {
    for (int i = 0; i < len; i++) {
        size_t len = strlen(array[i]) + 1;

        char *tempStr = malloc(len); // allocate space for the string + null terminator
        if(tempStr == NULL) {        // malloc failed
            perror("malloc");
            exit(1);
        }
        memcpy(tempStr, array[i], len);

        if (strcmp(array[i], strrev(tempStr)) == 0) {
            printf("%s is palindrome\n", array[i]);
        } else {
            printf("%s is not palindrome\n", array[i]);
        }

        free(tempStr);               // free the allocated memory
    }
}

Alternatively, do not reverse the strings at all. Check if a string is a palindrome by just comparing the beginning with the end and "walk towards the middle":

#include <stdbool.h>

bool is_palindrome(const char* str) {
    for (const char *b = str, *e = b + strlen(str); b < e--; ++b) {
        if(*b != *e) return false;
    }
    return true;
}

void palindChecker(const char *array[], int len) {
    for (int i = 0; i < len; i++) {
        if (is_palindrome(array[i])) {
            printf("%s is palindrome\n", array[i]);
        } else {
            printf("%s is not palindrome\n", array[i]);
        }        
    }
}
暮光沉寂 2025-01-31 08:47:38

重新加载时,我知道看到 @ted lyngmo is_palindrome(const char* str)。

社区wiki 离开。


什么都没有输出

strrev(*(array + i)尝试在就位更改a string literal lirital lirtal “ rar”。导致不确定的行为(UB)Whihc包括不打印

。开始。

#include <stdbool.h>

bool is_palindrome(const char *s) {
  const char *end = s;
  while (*end) {
    end++;
  }
  // end points to a null character.

  while (end > s) {
    --end;
    if (*end != *s) {
      return false;
    }
    s++;
  }
  return true;
}

void palindChecker(const char **array, int len) {
  for (int i = 0; i < len; i++) {
    if (is_palindrome(array[i]) {
      printf("%s is palindrome\n", *(array + i));
    }
    else {
      printf("%s is not palindrome\n", *(array + i));
    }
  }
}

On re-loading I know see @Ted Lyngmo good update with is_palindrome(const char* str).

Leaving as Community wiki for reference.


nothing gets outputted

strrev(*(array + i) attempts an in-place change of contents of a string literal like "rar". This leads to undefined behavior (UB) whihc includes not printing. Re-code and avoid UB.

Instead consider not reversing the string at all and simple compare the end of a spring to the beginning.

#include <stdbool.h>

bool is_palindrome(const char *s) {
  const char *end = s;
  while (*end) {
    end++;
  }
  // end points to a null character.

  while (end > s) {
    --end;
    if (*end != *s) {
      return false;
    }
    s++;
  }
  return true;
}

void palindChecker(const char **array, int len) {
  for (int i = 0; i < len; i++) {
    if (is_palindrome(array[i]) {
      printf("%s is palindrome\n", *(array + i));
    }
    else {
      printf("%s is not palindrome\n", *(array + i));
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文