From c535fb5c82c14491113b70f8d5ccdf19fc6d2b45 Mon Sep 17 00:00:00 2001 From: Ashwek Swamy <39827514+ashwek@users.noreply.github.com> Date: Fri, 21 Dec 2018 20:36:18 +0530 Subject: [PATCH] Day 2 (Reverse + Palindrome) - C (#28) * Create Reverse.py * Create Palindrome.py * Update README.md * Create Reverse.c * Create Palindrome.c * Update Reverse.c * Update Palindrome.c * Update README.md --- Day2/C/Palindrome.c | 33 ++++++++++++++++++++ Day2/C/Reverse.c | 30 ++++++++++++++++++ Day2/README.md | 76 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 Day2/C/Palindrome.c create mode 100644 Day2/C/Reverse.c diff --git a/Day2/C/Palindrome.c b/Day2/C/Palindrome.c new file mode 100644 index 00000000..32b79cba --- /dev/null +++ b/Day2/C/Palindrome.c @@ -0,0 +1,33 @@ +/** + * @author : ashwek + * @date : 21/12/2018 + */ + +#include +#include + +void reverse(char Str[]){ + char temp; + int i; + + for(i=0; i<(strlen(Str)/2); i++){ + temp = Str[i]; + Str[i] = Str[strlen(Str)-i-1]; + Str[strlen(Str)-i-1] = temp; + } +} + +void main(){ + + char Str[50], Rev[50]; + printf("Enter a string = "); + scanf("%s", Str); + + strcpy(Rev, Str); + reverse(Rev); + + printf("%s is ", Str); + if( strcmp(Str, Rev) != 0) + printf("not "); + printf("palindrome\n"); +} diff --git a/Day2/C/Reverse.c b/Day2/C/Reverse.c new file mode 100644 index 00000000..a6a5c94c --- /dev/null +++ b/Day2/C/Reverse.c @@ -0,0 +1,30 @@ +/** + * @author : ashwek + * @date : 21/12/2018 + */ + +#include +#include + +void reverse(char Str[]){ + char temp; + int i; + + for(i=0; i<(strlen(Str)/2); i++){ + temp = Str[i]; + Str[i] = Str[strlen(Str)-i-1]; + Str[strlen(Str)-i-1] = temp; + } +} + +void main(){ + + char Str[50], Rev[50]; + printf("Enter a string = "); + scanf("%s", Str); + + strcpy(Rev, Str); + reverse(Rev); + + printf("Reverse = %s", Rev); +} diff --git a/Day2/README.md b/Day2/README.md index 67f89952..da2417c3 100644 --- a/Day2/README.md +++ b/Day2/README.md @@ -267,7 +267,42 @@ int main() return 0; } ``` +## C Implementation +### [Solution 1](./C/Reverse.c) + +```c +/** + * @author : ashwek + * @date : 21/12/2018 + */ + +#include +#include + +void reverse(char Str[]){ + char temp; + int i; + + for(i=0; i<(strlen(Str)/2); i++){ + temp = Str[i]; + Str[i] = Str[strlen(Str)-i-1]; + Str[strlen(Str)-i-1] = temp; + } +} + +void main(){ + + char Str[50], Rev[50]; + printf("Enter a string = "); + scanf("%s", Str); + + strcpy(Rev, Str); + reverse(Rev); + + printf("Reverse = %s", Rev); +} +```
@@ -453,3 +488,44 @@ if( Str != Str[::-1] ): print("Palindrome") ``` + +## C Implementation + +### [Solution 1](./C/Palindrome.py) + +```c +/** + * @author : ashwek + * @date : 21/12/2018 + */ + +#include +#include + +void reverse(char Str[]){ + char temp; + int i; + + for(i=0; i<(strlen(Str)/2); i++){ + temp = Str[i]; + Str[i] = Str[strlen(Str)-i-1]; + Str[strlen(Str)-i-1] = temp; + } +} + +void main(){ + + char Str[50], Rev[50]; + printf("Enter a string = "); + scanf("%s", Str); + + strcpy(Rev, Str); + reverse(Rev); + + printf("%s is ", Str); + if( strcmp(Str, Rev) != 0) + printf("not "); + printf("palindrome\n"); +} + +``` \ No newline at end of file