TAB

C-Program To Reverse A String


C-Program To Reverse A String
Code 1:String reverse in c without using strrev

#include<stdio.h>
int main(){
    char str[50];
    char reverse[50];
    int i=-1,j=0;
    printf("Enter a string : ");
    scanf("%s",str);
   
    while(str[++i]!='\0');
    while(i>=0)
     reverse[j++] = str[--i];
    reverse[j]='\0';
  
    printf("Reverse of string is : %s",reverse);
  
    return 0;
}
output:
Enter a string :Fresherstation.blogspot.in
Reversed string is :ni.topsgolb.noitatsrehserf
 
Code 2:Reverse a string in c without using temp

#include<stdio.h>
#include<string.h>
 
int main(){
    char str[50];
    char *reverse;
    printf("Enter a string ");
    scanf("%s",str);
    reverse = strrev(str);
   
    printf("Reversed string is %s",reverse);
   
    return 0;
}
output:
Enter a string :Fresherstation.blogspot.in
Reversed string is :ni.topsgolb.noitatsrehserf
 
Code 3:Reverse a string in c using pointers

#include<stdio.h>
int main(){
    char str[50];
    char reverse[50];
    char *sptr = str;
    char *rptr = reverse;
    int i=-1;
    printf("Enter a string ");
    scanf("%s",str);
   
    while(*sptr){
     sptr++;
     i++;
    }
    while(i>=0){
     sptr--;
     *rptr = *sptr;
     rptr++;
     --i;
    }
    *rptr='\0';
  
    printf("Reversed string is : %s",reverse);
  
    return 0;
}
output:
Enter a string :Fresherstation.blogspot.in
Reversed string is :ni.topsgolb.noitatsrehserf

No comments:

Post a Comment

Related Posts

Related Posts Plugin for WordPress, Blogger...