From 49e51d94ff470e588a8e860208188f07d2893ff7 Mon Sep 17 00:00:00 2001 From: G-Dinusha Date: Sat, 19 Oct 2024 09:52:58 +0530 Subject: [PATCH] Update ReverseString.c --- C Program/ReverseString.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/C Program/ReverseString.c b/C Program/ReverseString.c index cce9f939d..9cfcad941 100644 --- a/C Program/ReverseString.c +++ b/C Program/ReverseString.c @@ -1,15 +1,28 @@ #include #include -int main() -{ - char s[100]; - printf("Enter a string to reverse\n"); - gets(s); +// Function to reverse a string +void reverseString(char *str) { + int n = strlen(str); + for (int i = 0; i < n / 2; i++) { + char temp = str[i]; + str[i] = str[n - i - 1]; + str[n - i - 1] = temp; + } +} + +int main() { + char s[100]; + + printf("Enter a string to reverse (max 99 characters): "); + fgets(s, sizeof(s), stdin); // Use fgets instead of gets + + // Remove the newline character if it exists + s[strcspn(s, "\n")] = 0; - strrev(s); + reverseString(s); // Call the custom reverse function - printf("Reverse of the string: %s\n", s); + printf("Reverse of the string: %s\n", s); - return 0; + return 0; }