Welcome to the World of Programming

Welcome to the World of Programming

Search this blog

Reverse String Using Linked List

/*********************************************************************
 * Program to reverse given string using Linked List
**********************************************************************/

#include "stdio.h"
#include "string.h"                        
#include "malloc.h"

typedef struct Rev{
char a;
struct Rev *link;
}Rev;

int main(){
Rev *head=NULL,*temp=NULL,*temp1=NULL,*temp2=NULL;
char b[20];
int i,j=0,len;
printf("Enter a String: ");
gets(b);
len=strlen(b);
for(i=0;i
if(head==NULL){
head=(Rev *)malloc(sizeof(Rev));
head->a=b[i];
head->link=NULL;
}
else
{
temp=head;
while(temp->link != NULL){
temp=temp->link;
}
temp->link=(Rev *)malloc(sizeof(Rev));
temp->link->link=NULL;
temp->link->a=b[i];
}

}
for(i=0;i
temp1=head;
temp2=temp1->link;
if(temp2 != NULL){
while(temp2->link != NULL){
temp2=temp2->link;
temp1=temp1->link;
}
printf("%c",temp2->a);
temp1->link=NULL;
}
else
{
printf("%c",temp1->a);
}
}
return 0;
}

2 comments:

  1. C++ Program to Reverse a Strings

    Reverse a String means reverse the position of all character of String. You can use swapping concept to reverse any string in c++.

    ReplyDelete