user5650078
user5650078

Reputation:

ERROR: Initializer is not a constant

int isPalindrome(char *str)
{
    static int length = strlen(str);
    if (length < 1)
        return 1;
    if (str[0] == str[length - 1])
    {
        length -= 2;
        return isPalindrome(str + 1);
    }
    else return 0;
}

int main() {
    char word[25];
    printf("Please enter a word: ");
    scanf("%s", word);
    if (isPalindrome(word))
        printf("%s is Palindrome", word);
    else
        printf("%s isn't Palindrome", word);
    return 0;
}

after i ran this code. it has error ERROR: Initializer is not a constant. at this line. What should I do?

static int length = strlen(str);

The function must have 1 argument(string of input) ==> isPalindrome(char *str)

Upvotes: 0

Views: 239

Answers (3)

Karthikeyan.R.S
Karthikeyan.R.S

Reputation: 4041

static is a like a global variable. You can't assign the dynamic value to the global variable. You can only assign the constant value to the global variable.

So try this,

static int length;
length = strlen(str);

Then In your case, you don't need to make that variable as a static.

Edited Code

int isPalindrome(char *str)
{
    int length = strlen(str);
    while(1){
            if (length < 1)
                    return 1;
            if (str[0] == str[length - 1])
            {
               length -= 2;
                str+=1;
            }
            else return 0;
    }
}


int main() {
     char word[25];
     printf("Please enter a word: ");
     scanf("%s", word);

     if (isPalindrome(word))
         printf("%s is Palindrome", word);
     else
         printf("%s isn't Palindrome", word);
    return 0;
 }                                            

Upvotes: 1

user5650078
user5650078

Reputation:

int isPalindrome(char *str)
{
    int length = strlen(str);
    if (length <= 1)
        return 1;
    if (str[0] == str[length - 1])
    {
        str[length - 1] = '\0';
        length -= 2;
        return isPalindrome(str + 1);
    }
    else return 0;
}

int main() {
    char word[25];
    printf("Please enter a word: ");
    scanf("%s", word);
    if (isPalindrome(word))
        printf("%s is Palindrome\n", word);
    else
        printf("%s isn't Palindrome\n", word);
    system("pause");
    return 0;
}

This code is OKAY. but it has a little bit bug.

Please enter a word: abcba
abc is Palindrome

answer is correct. but string is not correct (I can't edit main. I can edit only other function)

Upvotes: 0

P.P
P.P

Reputation: 121387

Any object with static storage duration can only be initialized with constant expressions. strlen(str) is not a constant expression.

§ 6.7.9, Initialization

All the expressions in an initializer for an object that has static or thread storage duration shall be constant expressions or string literals.

Instead, you can remove the static qualifier and re-write the logic by simply using additional variables:

int isPalindrome(char *str, size_t st, size_t end)
{  
    if (st >= end) return 1;
    return (str[st] == str[end]) && isPalindrome(str, st+1, end-1);
}

and call:

if (isPalindrome(word, 0, strlen(word) - 1))

In your existing implementation, you need to change the NUL terminator and remove static:

int isPalindrome(char *str)
{
    int length = strlen(str);
    if (length < 1)
        return 1;
    if (str[0] == str[length - 1])
    {
        str[length - 1] = '\0';
        length -= 2;
        return isPalindrome(str + 1);
    }
    else return 0;
}

Make a copy of word and pass it:

   char temp[25];
   strcpy(temp, word);
   if (isPalindrome(temp)) {

Upvotes: 1

Related Questions