我正在尝试将"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]!=' ')
{
if((str[i]==' ')||(str[i]=='n'))
{
word[j]=' ';
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##%sn",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): %dn",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;
}
感谢
代码有很多错误:
您正在使用一个单词数组来读取所有单词。因此,当您读取"Hello"时,您读取单词数组,打印"##Hello",并将指向单词数组的指针存储为front->info。然后,用World重写单词数组。此外,请注意,您永远不会添加带有单词"World"的节点,因为一旦遇到"\0"就会退出循环。因此,您的链表只包含一个节点。但是,有一个问题,因为你在第一个节点中存储了一个指向单词数组的指针,并且单词数组已经被"World"覆盖,所以当你退出循环时,列表中只有一个节点,这个节点的信息是单词数组,它包含"World",而不是以前的"Hello"。所以,我想这解释了输出?
您应该能够为此目的使用strtok()
。请参阅此示例,只需将标签替换为空格并向后打印即可。这是迄今为止实现这一目标最简单的方法。
看起来像作业。。。但是,对于初学者来说,如果你的分隔符是一个空格和一条换行符:
if((str[i]==' ')||(str[i]=='n'))
那么结尾不包含空格或换行符的字符串将永远不会解析最后一个元素:
str= "Hello World"; //fgets(msg,sizeof(msg),stdin);
所以我的猜测是,你甚至从来没有把"世界"列入链接列表。
最后我做了这个
/**
I am a boy -> boy a am I
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int i, j, n, temp, temp_i, cnt;
//char *array = "Samsung";
char array[1000];
char newarr[strlen(array)];
printf("Enter The String: n");
gets(array);
for(i = (strlen(array)-1), n = 0, j = 0; i >= 0; i--)
{
if( array[i] != ' ')
{
n++;
}
else
{
temp = n;
temp_i = i;
for(n = 0; n <= temp; n++)
{
// i = i + 1;
newarr[j++] = array[i++];
}
i = temp_i;
n = 0;
}
if(i == 0)
{
newarr[j++] = ' ';
temp = n;
temp_i = i;
for(n = 0; n <= temp; n++)
{
// i = i + 1;
newarr[j++] = array[i++];
}
i = temp_i;
n = 0;
}
//newarr[j++] = array[i];
}
newarr[j] = ' ';
cnt = 0;
for(j = 0; j <= (strlen(newarr)-1); j++)/*This is not required just do some R n D*/
{
newarr[j] = newarr[++cnt];
}
// printf("The first element is %c n", newarr[1]);
puts(newarr);
return 0;
}
这里有一个使用c++11的解决方案。它根据需要反转单词并将其打印在屏幕上。
vector<string> words;
string str = "hello world c++11";
size_t current = 0;
size_t found = str.find(" ");
while(found != string::npos)
{
words.push_back(str.substr(current, found - current));
current = found + 1;
found = str.find(" ",current);
}
words.push_back(str.substr(current));
std::ostream_iterator<string> Display_iter(std::cout," ") ;
std::copy(words.rbegin(), words.rend(), Display_iter);
1)首先反转整个字符串(它给出的结果类似于"dlrow olleh")2) 然后从第一个字符调用/反转单词,直到遇到空格/endOfString。3) 它提供所需的输出。
#include#包括
int main(){
char *src = "I am a boy";
char dest[50][50];
int idx = 0;
int priv_idx = 0;
int i = 0;
int j = 0;
while(src[i] != ' ') {
if(src[i] == ' ') {
if(priv_idx == idx) {
idx ++;
j = 0;
}
i++;
continue;
}
*(*(dest + idx) + j) = src[i];
i++;
j++;
priv_idx = idx;
}
for (i = idx; i>=0; --i) {
printf("%snr",dest[i]);
}
return 0;
}
#include <stdio.h>
#include <string.h>
#define MAX_ROW 50
#define MAX_COLUMN 50
char dest[MAX_ROW][MAX_COLUMN];
int str_rev_order(char *src)
{
int idx = 0;
int priv_idx = 0;
int i = 0;
int j = 0;
for(i = 0;i<MAX_ROW; ++i) {
memset(dest[i],0,MAX_COLUMN);
}
/* reset the counter */
i = 0;
while(src[i] != ' ') {
if(idx >= MAX_ROW-1) {
printf("Don't support more than %d substring.nr",MAX_ROW);
return -1;
}
if(j >= MAX_COLUMN -1) {
printf("Don't support string length more than %d.nr",MAX_COLUMN);
return -1;
}
if(src[i] == ' ') {
if(priv_idx == idx) {
/* going to next row & reset the column counter*/
idx ++;
j = 0;
}
i++;
continue;
}
*(*(dest + idx) + j) = src[i];
i++;
j++;
priv_idx = idx;
}
return idx;
}
void print_rev_order(int idx) {
int i;
for (i = idx; i>=0; --i) {
printf("%snr",dest[i]);
}
}
int main() {
char *src = "I am a boy";
int idx = str_rev_order(src);
print_rev_order(idx);
return 0;
}