V
vijay
Hello,
As the subject suggests, I need to print the string in the reverse
order. I made the following program:
# include<stdio.h>
struct llnode
{
char *info;
struct llnode *next;
};
typedef struct llnode NODE;
int main()
{
char msg[50],word[10],*str;
int i=0,length=0,j=0;
NODE *ptr,*front=NULL,*temp,*last=NULL;
printf("Enter the sentence: ");
str=fgets(msg,sizeof(msg),stdin);
while(str!='\0')
{
if((str==' ')||(str=='\n'))
{
word[j]='\0';
j=0;
ptr=(NODE *)malloc(sizeof(NODE));
ptr->info=word;
ptr->next=NULL;
if(front==NULL)
{
front=ptr; // only change the value of
front here
}
else
{
temp=front;
while((temp->next)!=NULL)
{
temp=temp->next;
}
temp->next=ptr;
}
printf("\n##%s\n",front->info); // prints the
words and not //
the first word
}
else
{
word[j]=str;
j++;
}
i++;
}
temp=front;
while(temp)
{
length++;
printf("%s ",temp->info);
temp=temp->next;
}
printf("\nLength of Linked List(or, number of words):
%d\n",length);
i=0;
printf("\n************************\n");
while(i<length)
{
temp=front;
while(temp->next!=last)
{
temp=temp->next;
}
last=temp;
printf("%s ",temp->info);
i++;
}
return 0;
}
Here, front is a poiter to the first node. But when I print
front->info, I get different words, I mean "Hello" once and "World",
the second time. I nowhere change the value of fromt except in the
first if statement.
The length of the linked list is printed correctly. And sorry for
pasting such a big code in the post itself, I had no other option.
The output is:
[vijay@vijay ds]$ ./a.out
Enter the sentence: Hello World
##Hello
##World
World World
Length of Linked List(or, number of words): 2
************************
World World [vijay@vijay ds]$
regards,
vijay.
As the subject suggests, I need to print the string in the reverse
order. I made the following program:
# include<stdio.h>
struct llnode
{
char *info;
struct llnode *next;
};
typedef struct llnode NODE;
int main()
{
char msg[50],word[10],*str;
int i=0,length=0,j=0;
NODE *ptr,*front=NULL,*temp,*last=NULL;
printf("Enter the sentence: ");
str=fgets(msg,sizeof(msg),stdin);
while(str!='\0')
{
if((str==' ')||(str=='\n'))
{
word[j]='\0';
j=0;
ptr=(NODE *)malloc(sizeof(NODE));
ptr->info=word;
ptr->next=NULL;
if(front==NULL)
{
front=ptr; // only change the value of
front here
}
else
{
temp=front;
while((temp->next)!=NULL)
{
temp=temp->next;
}
temp->next=ptr;
}
printf("\n##%s\n",front->info); // prints the
words and not //
the first word
}
else
{
word[j]=str;
j++;
}
i++;
}
temp=front;
while(temp)
{
length++;
printf("%s ",temp->info);
temp=temp->next;
}
printf("\nLength of Linked List(or, number of words):
%d\n",length);
i=0;
printf("\n************************\n");
while(i<length)
{
temp=front;
while(temp->next!=last)
{
temp=temp->next;
}
last=temp;
printf("%s ",temp->info);
i++;
}
return 0;
}
Here, front is a poiter to the first node. But when I print
front->info, I get different words, I mean "Hello" once and "World",
the second time. I nowhere change the value of fromt except in the
first if statement.
The length of the linked list is printed correctly. And sorry for
pasting such a big code in the post itself, I had no other option.
The output is:
[vijay@vijay ds]$ ./a.out
Enter the sentence: Hello World
##Hello
##World
World World
Length of Linked List(or, number of words): 2
************************
World World [vijay@vijay ds]$
regards,
vijay.