C program to reverse the string
We will see two methods to reverse the string
1. Reverse the string without using function
2. Reverse the string using function
1. Reverse the string without using function
#include <stdio.h>
#include<string.h>
int main()
{
char str[20];
int size,i,j,temp;
printf("Enter the string:\n");
scanf("%[^\n]%*c",str);
size=strlen(str);
j=size-1;
for(i=0;i<j;i++)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
j--;
}
printf("Reverse the string:\n %s",str);
return 0;
}
Output:
Enter the string:
All The Best
Reverse the string:
tseB ehT llA
2. Reverse the string using function
#include <stdio.h>
#include<string.h>
void reverse(char*);
int main() {
char string[20],*b;
printf("Enter the string\n:");
scanf("%[^\n]%*c",string);
b=string;
reverse(b);
printf("reverse the string:\n%s",string);
return 0;
}
void reverse(char*str)
{
int i,temp,size,j;
size=strlen(str);
j=size-1;
for(i=0;i<j;i++)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
j--;
}
}
Output: Enter the string:
All The Best
reverse the string:
tseB ehT llA
Post a Comment
Post a Comment