If / else if logic error


My code looks good but I don't see issue here . I look at other examples and I don't see an issue with my code


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
    Name : Nathan Seto Kaiba

    Email: New_Nathan@msn.com

    Date : 04/01/2012

    Program : String Use (palindrome ) 

    Description : The goal of the program is to check the user input word is or is
                  not a palindrome . This program use many string functions from the 
                  ( string.h) . That will be explain with comments.     
*/

#include <stdio.h>  // Standard Input and Output header file  
#include <stdlib.h> // Use for ( fget) function   
#include <string.h> // Use for many string functions such as (strlwr) 
#include <ctype.h>  // Not used but include out of habit 


int main ( void )
{


  /*

     The two array have 21 instead of 20 because of the "\0" char to end array   

  */
  char UserWord [21]; // char array for user input sentence 
  char ReverseUserWord [21]; // char array to be reverse with the (strrev)

  printf(" Welcome User to the palindrome program \n\n\a" ); 
  printf(" This program only accept a word of 20 character \n\n " ); 

  printf(" ------------------------------------------------------ \n\n" );

  printf(" Please type in your word :\n\n " );
  printf(" ? " );  // Display to prompt user for input 
  fgets( UserWord , 20, stdin ); // used instead of (scanf) for security issues of string hack attack 
                                 // (stdin) found in (fgets) is to store the content in keyboard   

  strlwr( UserWord );  // Lower case complete word to deal with issues of upper case " racecar" and " Racecar" 

  strcpy(ReverseUserWord , UserWord); // Copy string content from Userword to ReverseUserWord

  strrev( ReverseUserWord ); // Reverse the content of char array ReverseUserWord 

  if (strcmp (ReverseUserWord ,UserWord ) == 0) // When String compare function return 0 Display below message 
  {
    printf ( " This word is a palindrome\n\n " ); 

  }
  else // Not a palindrome display the message below 
  {
    printf( " This word is not a palindrome\n\n\a " );

  }
  
  getchar(); // is there a better way to hault program then getchar ? 
             /* What do duct tape and the force have in common ? 
                they both have a dark and light side and hold the universe together */

  getchar();
  return 0;  // Program end 

} 
1
2
/* What do duct tape and the force have in common ? 
                they both have a dark and light side and hold the universe together */

Lol?
What's the problem with the code anyways? Looking at it on-the-fly i don't see any error.
About getchar... http://www.cplusplus.com/forum/articles/7312/#msg33733
I don't see any obvious error with the program.

The way to debug is to temporarily place the following lines at strategic places in the program

1
2
printf ("["); printf ( UserWord ); printf  ("]\n");
printf ("["); printf ( ReverseUserWord ); printf  ("]\n");
Sorry I forgot I Left that . My teacher tells us just to use
1
2
getchar();
getchar();


and I don't like it . It feels like puting ducktape in a hole rather than just doing it right.

for C++ to hault program I was taught to do the following
1
2
3
4

cin.ignore ( 100 , '\n'); 
getchar();


The link seem helpful so thank you.


To the program when I complie it . There no errors but doesn't seem to do the if/ else logic I need it to do.

It works fines when I use ( scanf) instead of fgets on line 40

Well here the sorce code of the change on line 40



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

/*
    Name : Nathan Seto Kaiba

    Email: New_Nathan@msn.com

    Date : 04/01/2012

    Program : String Use (palindrome )

    Description : The goal of the program is to check the user input word is or is
                  not a palindrome . This program use many string functions from the
                  ( string.h) . That will be explain with comments.
*/

#include <stdio.h>  // Standard Input and Output header file
#include <stdlib.h> // Use for ( fget) function
#include <string.h> // Use for many string functions such as (strlwr)
#include <ctype.h>  // Not used but include out of habit

# define SIZE 21


int main ( )
{


  /*

     The two array have 21 instead of 20 because of the "\0" char to end array

  */
  char UserWord [SIZE]; // char array for user input sentence
  char ReverseUserWord [SIZE]; // char array to be reverse with the (strrev)

  printf(" Welcome User to the palindrome program \n\n\a" );
  printf(" This program only accept a word of 20 character \n\n " );

  printf(" ------------------------------------------------------ \n\n" );

  printf(" Please type in your word :\n\n " );
  printf(" ? " );  // Display to prompt user for input
  scanf( "%20s" , UserWord );

  /*

     Note : Program would not function with fget function. Wasn't able to discover why ?
            Therefore used scanf with 20 as the limit char input .

            fget( UserWord , 20 , stdin );

  */



  strlwr( UserWord );  // Lower case complete word to deal with issues of upper case " racecar" and " Racecar"

  strcpy(ReverseUserWord , UserWord); // Copy string content from Userword to ReverseUserWord

  strrev( ReverseUserWord ); // Reverse the content of char array ReverseUserWord

  if (strcmp (ReverseUserWord ,UserWord ) == 0) // When String compare function return 0 Display below message
  {
    printf ( " This word is a palindrome\n\n " );

  }
  else  // Not a palindrome display the message below
  {
    printf( " This word is not a palindrome\n\n\a " );

  }

  getchar(); // is there a better way to hault program then getchar ?
             /* What do duct tape and the force have in common ?
                they both have a dark and light side and hold the universe together */

  getchar();
  return 0;  // Program end

}

Topic archived. No new replies allowed.