-4

i have search and found a solution but i cant figure it out why my code still not working, if i understand it right my check2 string not having '\0'. im using visual studio i know there is a better way to write and short then this but just tried to practice some things and tried to do it harder then its need to be. im new to C so dont judge me :P this is my code:

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

/*
A palindrome is a string that is same in both forward and backward reading.
Example:
   "madam"
   "racecar"
   "a man a plan a canal panama"
   "radar"
You will write a program that will test if a given string is a palingdrome or not.
Your program will ask the user to input a string and if the string is a palindrome program
will just print "Yes, it is a Palindrome", otherwise will print "No, not a Palindrome".

Please note that:
1. Your you need to check in case-insensitive way, that means: Madam or madam both should be
detected as Palindrome.

2. There can be (any number of ) spaces in between the words.
    "A man a plan a canal panama"
        OR
    "A     man    a   pla n a cana l Panama"
    both the strings must be detected as Palindrome.
3.There can be punctuations in between the words, for this assignments,
we consider only 4 punctuations,   . ?  ! and ,

Your program will just need to ignore them (treat them as space).
    "Cigar? Toss it in a can. It is so tragic."
    Should be detected as palindrome.

 *** For this assignment I will not write any instructions or guidance, you are free
        to implement it with your own way, you can use the string.h functions

    Good luck.

*/


int main(){
    char string [100];
    printf("Enter a string: ");
    scanf("%[^\n]", string);

    int isPalindrome = 1;  // assign 0 to this if the string is a NOT palindrome

    // write code to test if string is a palindrome

    char check1[100], check2[100];
    int i, j = 0;

    for (i = 0; string[i] != '\0'; i++, j++)
    {
        if ((string[i] >= 'A' && string[i] <= 'Z') || (string[i] >= 'a' && string[i] <= 'z'))
        {
            check1[i] = string[i];
        }
    }

    check1[i] = '\0';

    for (i = strlen(string); i < 0; i--)
    {
        if ((string[i] >= 'A' && string[i] <= 'Z') || (string[i] >= 'a' && string[i] <= 'z'))
        {
            check2[i] = string[i];
        }
    }

    i = strlen(string);
    check2[i] = '\0';

    for (i = 0; check1[i] != '\0'; i++)
    {
        if ((check1[i] >= 65) && (check1[i] <= 90))
            check1[i] = check1[i] + 32;
    }

    for (i = 0; check2[i] != '\0'; i++)
    {
        if ((check2[i] >= 65) && (check2[i] <= 90))
            check2[i] = check2[i] + 32;
    }

    printf("%s\n", check1);
    printf("%s\n", check2);

    isPalindrome = strcmp(check1, check2);

    // at the end you need to test
    if (isPalindrome){
        printf("Yes, it is Palindrome!\n");
    }
    else{
        printf("No, not a Palindrome\n");
    }


    return 0;
}

and this is my output:

Enter a string: MadaM
madam
╠╠╠╠╠
Yes, it is Palindrome!
Press any key to continue . . .

ok so now my code look like that:

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

/*
A palindrome is a string that is same in both forward and backward reading.
Example:
   "madam"
   "racecar"
   "a man a plan a canal panama"
   "radar"
You will write a program that will test if a given string is a palingdrome or not.
Your program will ask the user to input a string and if the string is a palindrome program
will just print "Yes, it is a Palindrome", otherwise will print "No, not a Palindrome".

Please note that:
1. Your you need to check in case-insensitive way, that means: Madam or madam both should be
detected as Palindrome.

2. There can be (any number of ) spaces in between the words.
    "A man a plan a canal panama"
        OR
    "A     man    a   pla n a cana l Panama"
    both the strings must be detected as Palindrome.
3.There can be punctuations in between the words, for this assignments,
we consider only 4 punctuations,   . ?  ! and ,

Your program will just need to ignore them (treat them as space).
    "Cigar? Toss it in a can. It is so tragic."
    Should be detected as palindrome.

 *** For this assignment I will not write any instructions or guidance, you are free
        to implement it with your own way, you can use the string.h functions

    Good luck.

*/


int main(){
    char string [100];
    printf("Enter a string: ");
    scanf("%[^\n]", string);

    int isPalindrome = 1;  // assign 0 to this if the string is a NOT palindrome

    // write code to test if string is a palindrome

    char check1[100], check2[100];
    int i, j, dess = 0, len;

    for (i = 0; string[i] != '\0'; i++)
    {
        if ((string[i] >= 'A' && string[i] <= 'Z') || (string[i] >= 'a' && string[i] <= 'z'))
        {
            check1[dess++] = string[i];
        }
    }

    check1[dess] = '\0';



    /*
    len = strlen(string);


    for (j = 0, i = 0; j < len; j++)
    {
        if ((string[j] >= 'A' && string[j] <= 'Z') || (string[j] >= 'a' && string[j] <= 'z'))
        {
            check2[i++] = string[len - j];
        }
    }
    */

    for (dess = 0, i = strlen(string) - 1; i >= 0; i--)
    {
        if ((string[i] >= 'A' && string[i] <= 'Z') || (string[i] >= 'a' && string[i] <= 'z'))
        {
            check2[dess++] = string[i];
        }
    }

    check2[dess] = '\0';

    for (i = 0; check1[i] != '\0'; i++)
    {
        if ((check1[i] >= 65) && (check1[i] <= 90))
            check1[i] = check1[i] + 32;
    }   

    for (i = 0; check2[i] != '\0'; i++)
    {
        if ((check2[i] >= 65) && (check2[i] <= 90))
            check2[i] = check2[i] + 32;
    }

    printf("%s\n", check1);
    printf("%s\n", check2);
    for (i = 0; check1[i] != '\0'; i++)
    {
        printf("%c\n", check1[i]);
    }
    for (i = 0; check2[i] != '\0'; i++)
    {
        printf("%c\n", check2[i]);
    }

    isPalindrome = strcmp(check1, check2);

    // at the end you need to test
    if (isPalindrome){
        printf("Yes, it is Palindrome!\n");
    }
    else{
        printf("No, not a Palindrome\n");
    }


    return 0;
}

but my output is:

Enter a string: MadaM
madam
madam
m
a
d
a
m
m
a
d
a
m
No, not a Palindrome
Press any key to continue . . .

why its say its not a palindrome?

  • 2
    `for (i = strlen(string); i < 0; i--)` never executes: the `i<0` condition is wrong. Should be `i>=0`. And should start at `strlen(string)-1` – Jean-François Fabre Aug 18 '18 at 10:35
  • `╠╠╠╠╠` is a magic numner, indicating a certain widely spread mistake in the code. There is a duplicate question, which admittefly I cannot find now. – Yunnosch Aug 18 '18 at 10:38
  • 1
    ah, I think you are looking for this Question here @Yummosch https://stackoverflow.com/questions/34931221/my-output-has-some-weird-symbols-displaying – Mukesh A Aug 18 '18 at 10:43
  • same symptoms, different cause. – Jean-François Fabre Aug 18 '18 at 10:45
  • 2
    ╠ is 0xCC in codepage 437, and [MSVC fills 0xCC to uninitialized memory to help debugging](https://stackoverflow.com/q/370195/995714). That means you've accessed uninitialized memory. You can find tons of questions about ╠ and 0xCC here on SO – phuclv Aug 18 '18 at 10:51
  • thanks guys idk why didnt find that post sorry. – Omer Biton Aug 18 '18 at 11:09
  • @MukeshA No. I found that one. I am of course looking for an answer, which explains the special meaning of the magic number. (Oh and my nick does not contain "m"s.) – Yunnosch Aug 18 '18 at 11:26
  • @phuclv That one is close and definitly helpful. But I am sure to have seen an answer which uses those non-alpha characters in the explanation. – Yunnosch Aug 18 '18 at 11:28

2 Answers2

0

this loop is not executing at all:

for (i = strlen(string); i < 0; i--)
{
    if ((string[i] >= 'A' && string[i] <= 'Z') || (string[i] >= 'a' && string[i] <= 'z'))
    {
        check2[i] = string[i];
    }
}

because i < 0 condition is a "continue" condition, not a "stop" condition. Besides, i starts outside string data.

So check2 is never initialized, which explains the garbage.

The fixed loop is:

for (i = strlen(string) - 1; i >= 0; i--)

Those reverse loop constructs are error prone. I'd suggest a forward loop and compute the reverse index in the loop, much clearer, like this:

int len = strlen(string);
for (j = 0; j < len; j++)
{
    i = len-j-1;   // starts at the last char, ends at the first char

in that case, there's no need to loop in reverse, since a forward loop achieves exactly the same result (there is no break or other index here)

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • that forward loop its much better but still i want to stay with my reverse loop now i have changed all working but when i put MadaM its said its not a Palindrome my code for (dess = 0, i = strlen(string) - 1; i >= 0; i--) { if ((string[i] >= 'A' && string[i] <= 'Z') || (string[i] >= 'a' && string[i] <= 'z')) { check2[dess++] = string[i]; } } – Omer Biton Aug 18 '18 at 11:48
0

You have some problems with your loop indizes and limits as pointed out by others.

Apart from that you have some problem with filtering for letters:

    for (i = 0; string[i] != '\0'; i++, j++)
    {
        if ((string[i] >= 'A' && string[i] <= 'Z') || (string[i] >= 'a' && string[i] <= 'z'))
        {
            check1[i] = string[i];
        }
    }

First of all, you could use function isalpha() to check for letters. Second, what do you think, happens to the spaces in between? You simply copy any letter to your array check just in the same place as where it is in your input string. This does not make sense.

You need a second variable for destination index::

    int dest = 0;
    for (i = 0; string[i] != '\0'; i++)
    {
        if ((string[i] >= 'A' && string[i] <= 'Z') || (string[i] >= 'a' && string[i] <= 'z'))
        {
            check1[dest++] = string[i];
        }
    }

Similar for the other loop(s)

Gerhardh
  • 11,688
  • 4
  • 17
  • 39
  • this is already better than my answer that fixes OP first issue... but doesn't fix the rest. Well, maybe we should close that question altogether with a duplicate link – Jean-François Fabre Aug 18 '18 at 10:50
  • Well, the dupe only tells that there is some uninitialized variable involved without solving anything for the OP. This would ask for an immediate follow up question "Where do I miss initialization ?" – Gerhardh Aug 18 '18 at 10:54
  • with the answers here + the duplicate OP has it sorted out, and this question doesn't really adds value to the site. – Jean-François Fabre Aug 18 '18 at 10:55
  • 1
    Yes, if the answers are kept it should be sufficient – Gerhardh Aug 18 '18 at 10:55
  • ohh you right didnt think about that, so i changed my code like all the advice here, when i print the strings its print the same, but when i put MadaM, its said its not a Palindrome. – Omer Biton Aug 18 '18 at 11:54