Revision as of 18:27, 13 May 2024 by Admin (Created page with "Write a program that prompts the user for a string (pick a maximum length), and prints its reverse.")
ABy Admin
May 13'24
Exercise
Write a program that prompts the user for a string (pick a maximum length), and prints its reverse.
ABy Admin
May 13'24
One possible solution could be:
#include <stdio.h>
#include <string.h>
int main(void)
{ char s[81]; // A string of upto 80 chars + '\0'
int i;
puts("Please write a string: ");
fgets(s, 81, stdin);
puts("Your sentence in reverse: ");
for (i= strlen(s)-1; i >= 0; i--)
{
if (s[i] == '\n')
continue; // don't write newline
else
putchar(s[i]);
}
putchar('\n');
return 0;
}