Home > database >  nothing is printing after running the below C code , nothing gets outputted
nothing is printing after running the below C code , nothing gets outputted

Time:04-26

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

CodePudding user response:

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]);
        }        
    }
}

CodePudding user response:

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));
    }
  }
}
  • Related