0

Hi!

Having some problems with functions in this one. As ypu can see I'm a tottal beginner. What I'm trying to do here is make a program checking if a word is a palindrome. Using 2 functions, one to check and one to reverse the user input string. My problem is simple, yet to hard for me. I can't seem to get to grips with the use of functions and how to call them.
The code I've written so far looks like this. Any help or pointers would be very appreciated.

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

bool palindromeCheck (char str1[], char str2[])
{
     int i=0;
     bool equal;
        
     while (str1[i] == str2[i] && str1[i] != '\0' && str2[i] != '\0')
     i++;
     
     if (str1[i] == '\0' && str2[i] =='\0')
        equal = true;
     else
         equal = false;
         
     return equal;
     }
     
char strReverse(char str1[], char str2[])
{
      int i=0, j=0;
      while(str1 != '\0')
      j++;
      j--;
      while(i<=j)
      {
      char t = str1[i];
      str1[i++] = str2[j];
      str2[j--] = t;
      }
      
      return str2[100];
}     

int main (void)
{
    bool palindromeCheck (char str1[]);
    char strReverse;
    char str1[100];    
    char str2[100];
    int i;
    bool equal;    
    printf ("Enter word:");
    scanf (" %s", str1);
    
    strcpy (str1, str2);
     
    printf ("\n\n");
        
    if (equal=true)
    {
       printf ("Word is a plaindrome!\n");
       }
    else
    {
        printf ("Word is not a palindrome!\n");
        }

    system ("pause");
    return 0;
    
}
3
Contributors
2
Replies
5
Views
6 Years
Discussion Span
Last Post by WaltP
0

@OP

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

/* This function is fine */
bool palindromeCheck (char str1[], char str2[])
{
     int i=0;
     bool equal;
        
     while (str1[i] == str2[i] && str1[i] != '\0' && str2[i] != '\0')
     i++;
     
     if (str1[i] == '\0' && str2[i] =='\0')
        equal = true;
     else
         equal = false;
         
     return equal;
     }
     
/* Some modifications done here */
void strReverse(char str1[])
{
      int i=0, j=0;
      while (str1[j++] != '\0')
      	      ;
      j -= 2; /* Here you had given j-- */
	
      while (i <= j)
      {
      	char t = str1[i]; 
	str1[i++] = str1[j];
        str1[j--] = t;     
      }     
      
     
}     

int main (void)
{
    
   
    char str1[100];    
    char str2[100];   
    int i;
    bool equal;
    
    printf ("Enter word:");
    scanf ("%s", str1);
    
    strcpy (str2, str1);
    strReverse(str1);	 /* Reverse the same word. or can copy as you had done */
	
    equal = palindromeCheck(str1, str2);
     
    printf ("\n\n");
        
    if (equal==true)
    {
       printf ("Word is a plaindrome!\n");
       }
    else
    {
        printf ("Word is not a palindrome!\n");
        }

    
    return 0;
    
}
0

Where did you call strReverse() ?
Where did you call palindromeCheck() ?
Where does equal get set to T or F?

You have to actually try calling them, they don't just run automagically...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.