Я пытаюсь сделать «Hello World» «World Hello». Но код не работает должным образом, как я хотел, чтобы он себя вел. Смотрите ниже код:Обратить строку «Hello World» в «World Hello», что не так?
#include<stdio.h>
#include<string.h>
#include<stdlib.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= "Hello World"; //fgets(msg,sizeof(msg),stdin);
while(str[i]!='\0')
{
if((str[i]==' ')||(str[i]=='\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 thewords and not
//the first word
}
else
{
word[j]=str[i];
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;
}
Благодаря
является то, что домашние задания? – Ben
@Ben: Это не домашнее задание, но вчера я проходил через один веб-сайт, там я нашел этот вопрос. Так подумал написать программу. Но этого не происходит. –
Перемещенный код через SO - пожалуйста, не помещайте свой код на другие сайты, а затем ссылайтесь на них, мы хотим, чтобы SO был полезен, даже если весь остальной интернет попадает в черную дыру. О, да, и вам, возможно, захочется учиться на отступ немного лучше, я исправил это и для вас :-) – paxdiablo