0

When I build below code in Microsoft Visual Studio, it build successfully, but the execution is failed.The command console window tells the error is exist and the program is stopped.

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

int is_palindrome(char *str)
{
    int len;
    int halflen;
    len = strlen(str);

    if (len% 2 == 0)
        halflen = len / 2;
    else halflen = (len - 1)/2;
    for (int i=0; i < len; i++)
    {
        if ((str[i] <= 90) && (str[i] >= 65))
        {
            str[i] = str[i] + 32;
        }
    }

    for (int j=0; j < halflen; j++)
    {
        printf("str[j] = %c and str[len-1-j] = %c \n", str[j], str[len - 1 - j]);
        if (str[j] != str[len-1-j])
        {
            return 0;
        }       
    }
    printf("str is: %s\n", str);
    return 1;
}

#ifndef RunTests
int main()
{
    char *str = "deleveled";
    printf("%d", is_palindrome(str));
}
#endif

I find the problem is caused by below part. Can you tell why this part of code cause the execution error?

    for (int i=0; i < len; i++)
    {
        if ((str[i] <= 90) && (str[i] >= 65))
        {
            str[i] = str[i] + 32;
        }
    }
Krunal
  • 77,632
  • 48
  • 245
  • 261
Sunny Lei
  • 181
  • 1
  • 3
  • 15

0 Answers0