-2
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define FALSE 0
#define TRUE 1  

int alphabetic(char *string )

{
int i, valid;
valid = TRUE;
for ( i = 0; i < strlen(string); i++ )
{
    if ( toupper ( string[i] ) < 'A' || toupper (string[i] ) > 'Z' )
        valid = FALSE;
}
return valid;
}

int main()
{

char c, inputarray[10], temp[10];
int i = 0;
strcpy(temp, inputarray);

printf("%s Please enter string>");

while ( ( c = getchar () ) != '\n')

{
    if ( i < 9 ) 
        inputarray[i] = c;
    i++;
}

if ( i < 10 )
    inputarray[i] = '\0';
else 
{
inputarray[9] = '\0';
printf("String too long\n");
return;
}
printf("%s\n",inputarray);

if (! alphabetic (inputarray) )
{
    printf("Invalid input");
}

if (strcmp(strrev(inputarray),temp) == 0 )

printf("Palindrome\n");

else
printf("Not palindrome\n");

}

Trying this and still getting 'not palindrome' when input is a palindrome. It says 'stack around inputarray corrupted' when I run the program. Any ideas on how to fix it so reads palindrome and stop the input array being corrupted.

Armali
  • 18,255
  • 14
  • 57
  • 171
  • 1
    What are you trying to accomplish with `strcpy(temp, inputarray)`? Note that at the time you're running it, both `temp` and `inputarray` are uninitialized. It's likely that neither of them are good null terminated strings. – Scott Mermelstein Dec 06 '16 at 15:23
  • `strcpy(temp, inputarray)` is at the wrong position, it has to come after inputarray is filled. Now you are copying uninitialized data. – Karsten Koop Dec 06 '16 at 15:23
  • 1. Learn how to use a [debugger](http://stackoverflow.com/questions/2069367/how-to-debug-using-gdb). 2. Indent your code, please ! 3. use `stdbool.h`, don't use MACRO for `true or false`. And finally, read http://stackoverflow.com/help/how-to-ask – Stargateur Dec 06 '16 at 15:24
  • The purpose of strcpy(temp, inputarray) is to copy the inputarray to another temporary array and compare. The strcmp previously was uninitialised. Essentially I want to compare the inputarray with an array that is the exact reverse and if true print palindrome. – Absolutebeginnertoc Dec 06 '16 at 15:34
  • Have tried moving the strcpy(temp, inputarray) to below the printf("%s please enter a string>"), still the same effect. – Absolutebeginnertoc Dec 06 '16 at 15:35
  • Yes, but by putting it in line 3 of your main function, it's not doing your intended purpose. Put it right above the `strcmp` line, and you'll find you get better results. What's happening is, until you have the `'\0'` in the right place, copying one string to another is undefined behavior. The memory in those positions are arbitary. In fact, in this case, you're getting a "string" that's longer than ten characters, and is corrupting your stack (i.e., causing inputarray to come across as corrupted.). Never operate on initialized data. :-) – Scott Mermelstein Dec 06 '16 at 15:37

2 Answers2

1

Here is one possible implementation. I've tried to explain in comments as much as I can but feel free toleave a comment if there is something that's not clear.

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

int alphabetic(char *string)
{
    int i, valid;
    valid = true;
    for (i = 0; i < strlen(string); i++)
    {
        if (toupper(string[i]) < 'A' || toupper(string[i]) > 'Z')
        {
            valid = false;
            // break here we are done;
            break;
        }

    }
    return valid;
}

void printArray(char* str)
{
    printf("Array = ");
    for (int i = 0; i < strlen(str); ++i)
    {
        printf("%c", str[i]);
    }
    printf("\n");
}

bool isPalindrome(char* str1, char* str2)
{
    bool isValidPalindrome = true;
    int length = strlen(str1);
    if (length != strlen(str2))
    {
        printf("Strings must be the same lenth");
        isValidPalindrome = false;
    }
    else
    {
        --length;
        for (int i = length; i >= 0; --i)
        {
            if (str1[i] != str2[length - i])
            {
                isValidPalindrome = false;
                break;
            }
        }
    }
    return isPalindrome;
}

int main()
{
    const int length = 10;
    char c, inputarray[length], temp[length];
    int i = 0;
    // Comparing strings that have not been initialized
    // produces undefined behavior. Imagine inputArray is equal to:
    // inputArray: "my String ... some other unknown stuff"... where does
    // the string ends? there is no '\n' in the horizon.
    // The stack error you are getting is produced by the statement
    // below. I've pusehd this statement below right after inputArray
    // has been initialized
    // strcpy(temp, inputarray);

    // You don't need the format specifier %s unless you
    // rewrite your printf statement as printf("%s", "Please enter string");
    // for simplicity you can write it as follows
    printf("Please enter string: ");

    while ((c = getchar()) != '\n')
    {
        if (i < length - 1)
            inputarray[i] = c;
        i++;
    }

    // Pulled the code inside the if to avoid multiple returns // just preference... not needed
    if (i < length)
    {
        inputarray[i] = '\0';
        // helper function to print array
        printArray(inputarray);

        if (!alphabetic(inputarray))
        {
            printf("Invalid input");
        }
        // copy the strings here
        strcpy(temp, inputarray);
        // reverse the string here
        strrev(inputarray);
        // you will have to roll out your own isPalindrome
        // implementation since reversing a string and comparing it
        // with itself will always return false e.g.
        // inputArray = "hello";
        // copy inputArray into temp
        // temp = "hello";
        // reverse inputArray
        // compare strings: "olleh" == "hello" -> false
        if (isPalindrome(inputarray, temp) == true)
            printf("Palindrome\n");
        else
            printf("Not palindrome\n");
    }
    else
    {
        inputarray[9] = '\0';
        printf("String too long\n");
    }
    return 0;
}
StaticBeagle
  • 5,070
  • 2
  • 23
  • 34
0

Trying this and still getting 'not palindrome' when input is a palindrome. It says 'stack around inputarray corrupted' when I run the program.

The probable reason for both is that strcpy(temp, inputarray) is called before inputarray is entered. Move this immediately before the if (strcmp(strrev(inputarray),temp) == 0 ), and your program may work. Another error is the %s in printf("%s Please enter string>").

Armali
  • 18,255
  • 14
  • 57
  • 171