Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ReverseString.c #2965

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions C Program/ReverseString.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
#include <stdio.h>
#include <string.h>
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;
}