Program to Reverse a String in C Using While Loop
#include <stdio.h>
#include <string.h>
void main()
{
/*Write a Program to Reverse a String in C Using Loop */
char str[100], temp;
int i, j = 0;
printf("Enter The String: ");
gets(str);
i = 0;
j = strlen(str) - 1;
while (i < j)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
printf("\nReverse a String Is: %s\n\n", str);
return (0);
}
Reverse a String in C Using strrev
#include <stdio.h>
#include <string.h>
void main()
{
/*Write a Program to Reverse a String in C */
char str[100], temp;
int i, j = 0;
printf("Enter The String: ");
gets(str);
strrev(str);
printf("\nReverse a String in C Is: %s\n\n", str);
return (0);
}
Program to Reverse a String Using Recursion
#include <stdio.h>
#include <string.h>
void revstr(char *, int, int);
/*How to reverse a string in C Using Recursion */
int main()
{
char str[100];
printf("Enter The String: ");
gets(str);
revstr(str, 0, strlen(str) - 1);
printf("%s\n", str);
return 0;
}
void revstr(char *z, int start, int end)
{
char ch;
if (start >= end)
return;
ch = *(z + start);
*(z + start) = *(z + end);
*(z + end) = ch;
revstr(z, ++start, --end);
}
The Output of Reverse a String in C
How to Reverse a String in C
So we can Reverse a String to the full string, this is a similar problem to reversing the sentence, and we can solve this problem by using a strrev() library function. There are many ways to solve this problem, we can solve this problem using Using Pointers, strrev(), and Recursion in the C. For IT Professionals A complete list of Amazon AWS certification exam practice test questions is available to Pass the Exam on the First Attempt Easily.
Special Case: There is a special case for this problem we need to know about it. Let's take an example of it. As we can see that the below program has more than 1 space between the words so our output should be according to the spaces given in a String.
Input Format
Programming With Basics Website
Output Format
Reverse a String is: etisbeW scisaB htiW gnimmargorP
Instead, for using the library function always solve the problem with your own logic, in this way your logic building will be implemented
0 Comments: