Last active
July 20, 2020 14:18
-
-
Save raghunayak/46496341aac56e9e6e69f1beca727e80 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2020 Raghavendra Nayak | |
* | |
* Permission to use, copy, modify, and/or distribute this software for any purpose | |
* with or without fee is hereby granted. | |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | |
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
* FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | |
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS | |
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER | |
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF | |
* THIS SOFTWARE. | |
*/ | |
/* | |
* C program to reverse given string and to check whether it is a palindrome. | |
*/ | |
#include <stdio.h> | |
#include <string.h> | |
/** | |
* @brief function to return the given string length. | |
* @param[in] string the input string. | |
* @return the string length. | |
*/ | |
unsigned int get_string_length(const char *string) | |
{ | |
int i; | |
int string_length = 0; | |
for (i = 0; string[i] != 0 && string[i] != '\n'; ++i) | |
++string_length; | |
return string_length; | |
} | |
/** | |
* @brief function to reverse the given string. | |
* @param[in] string the input string. | |
* @param[out] rev_string the reversed string would be placed in this variable. | |
* @return none. | |
*/ | |
void reverse(const char *in_string, char *rev_string) | |
{ | |
int i, j; | |
unsigned int length; | |
length = get_string_length(in_string); | |
for (i = length-1, j = 0; i >= 0; --i, ++j) | |
rev_string[j] = in_string[i]; | |
/* mark the end of string */ | |
rev_string[j] = 0; | |
} | |
int main() | |
{ | |
const int max_size = 100; | |
int string_length = 0; | |
char string[max_size]; | |
char reverse_string[max_size]; | |
printf("\nPlease enter input string: "); | |
/* Note: we are not using gets, because gets is deprecated and can be | |
dangerous. */ | |
fgets(string, max_size, stdin); | |
/* Remove any new line from input string, | |
if fgets has put '\n' at the end of string */ | |
string_length = get_string_length(string); | |
string[string_length] = 0; | |
/* get the reversed string */ | |
reverse(string, reverse_string); | |
/* now compare */ | |
if (!strcmp(string, reverse_string)) { | |
printf("\nEntered string = %s, reversed string = %s." | |
"\n%s = %s, hence %s is a palindrome.\n", | |
string, reverse_string, string, reverse_string, string); | |
} else { | |
printf("\nEntered string = %s, reversed string = %s." | |
"\n%s != %s, hence %s is not a palindrome.\n", | |
string, reverse_string, string, reverse_string, string); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment