Write a program that reverses a string with the following features: 1. In the ma
ID: 3760915 • Letter: W
Question
Write a program that reverses a string with the following features: 1. In the main function, ask the user to enter a string. 2. The entered string can be handled as C-string (or converted to C-string). 3. Write a reverses tri function that takes the string parameter (C-string or char *) and reverse it. 4. In the main function, print out the original string and the reversed string. 5. The reverseString function must be implemented using pointer notation. 6. Using non-pointer notations to implement reverseString will result in a 3-point deduction. 7. Using library functions to do reserve will result in a 5-point deduction. (It is OK to use a function to get string?s length. It'd even better to count the length in the code).Explanation / Answer
#include<stdio.h>
#include <string.h>
void reverseString(char *str)
{
int length = (int)strlen(str);
int i=0,j=length-1;
char temp;
while(i<j)
{
temp = *(str+i);
*(str+i) = *(str+j);
*(str+j) = temp;
i++;
j--;
}
}
int main()
{
char *str;
printf("Input a string : ");
gets(str);
printf("String is : ");
puts(str);
reverseString(str);
printf("String after reversing is : ");
puts(str);
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.