why I am not getting any output even no errors so that I could figure out what changes should I made.
should I change the compiler?
Please help me in simple words thanks a lot.
#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) {
int tempStrLen, tempStrLen1;
for (int i = 0; i < len; i ) {
tempStrLen = strlen(array[i]);
if (tempStrLen1 <= tempStrLen) {
tempStrLen1 = tempStrLen;
}
}
char tempStr[tempStrLen1];
for (int i = 0; i < len; i ) {
strcpy(tempStr, strrev(array[i]));
if (strcmp(strrev(tempStr), array[i]) == 0) {
printf("%s is palindrome\n", array[i]);
}
else {
printf("%s is not palindrome\n", array[i]);
}
}
}
CodePudding user response:
Your code as multiple problems:
tempStrLen1
is not initialized when calculating the maximum string length- Once the maximum string length is calculated, the allocated array is too short (remember: a string additionally needs a terminating 0)
strrev()
reverses a string in place, i.e. it modifies the passed string. This will not work forarray[i]
as these strings are constants and are protected from modification.- Even if it worked, the code assumes that the original string is not modified.
The below code fixes the problems.
void palindChecker(char **array, int array_len) {
// calculate maximum string length
int str_len = 0;
for (int i = 0; i < array_len; i ) {
int len = strlen(array[i]);
if (str_len < len)
str_len = len;
}
char tempStr[str_len 1];
for (int i = 0; i < array_len; i ) {
// create modifiable copy
strcpy(tempStr, array[i]);
// reverse copy
strrev(tempStr);
if (strcmp(tempStr, array[i]) == 0) {
printf("%s is palindrome\n", array[i]);
}
else {
printf("%s is not palindrome\n", array[i]);
}
}
}
CodePudding user response:
You need to initialize initially the variable tempStrLen1
before the for loop
int tempStrLen, tempStrLen1;
for (int i = 0; i < len; i ) {
tempStrLen = strlen(array[i]);
if (tempStrLen1 <= tempStrLen) {
tempStrLen1 = tempStrLen;
}
}
like
int tempStrLen, tempStrLen1 = 0;
Otherwise the function invokes undefined behavior.
Also you need to define the variable length array tempStr with one more element like
char tempStr[tempStrLen1 1];
A more serious bug of the function that you are trying to apply the non-standard function strrev to a string literal that again invokes undefined behavior
strcpy(tempStr, strrev(array[i]));
You need to write instead
strcpy(tempStr, array[i]);
strrev( tempStr );
Pay attention to that to determine whether a string represents a palindrome there is no need to create its copy. Instead you could just write
for ( int i = 0; i < len; i )
{
size_ j = 0;
size_t n = strlen( array[i] );
while ( j < n / 2 && array[i][j] == array[i][n - j - 1] ) j;
if ( j == n / 2 ) {
printf("%s is palindrome\n", array[i]);
}
else {
printf("%s is not palindrome\n", array[i]);
}
}
CodePudding user response:
One addition to @Codo's answer. You don't need two for
loops in your function.
void palindChecker(char **array, int len)
{
for (int i = 0; i < len; i )
{
int tempStrLen = strlen(array[i]);
char tempStr[tempStrLen 1];
strcpy(tempStr, array[i]);
strrev(tempStr);
if (strcmp(tempStr, array[i]) == 0)
{
printf("%s is palindrome\n", array[i]);
}
else
{
printf("%s is not palindrome\n", array[i]);
}
}
}