【发布时间】:2013-12-13 14:41:59
【问题描述】:
这个程序是基于链表的。读入一个字符串,提取所有以换行符分隔的子字符串。
输入应该是:
hello world\ngood bye\nWhat a nice day!\n\0
那么,预期的输出应该是:
[hello world]->[good bye]->[What a nice day]->
但是,当我运行程序并输入:
hello world\ngood bye\nWhat a nice day!\n\0
我的输出是:
[hello world\ngood bye\nWhat a nice day!\n\0]->
我尝试将 NULL 字符分别读取为 '\' 和 'n',但无法处理。我该如何解决它,以打印出预期的输出?
newTB(char text[]); // 功能说明
函数 newTB 分配一个新的文本缓冲区并使用数组中给出的文本初始化其内容。输入数组中的行都以“\n”结尾。整个文本以 '\0' 结尾。
char *dumpTB (TB tb);
以下函数不会改变它们的 textbuffer 参数。分配并返回一个包含给定文本缓冲区中文本的数组。文本缓冲区的每一行都需要以 '\n' 终止(这包括最后一行)。整个文本必须以“\0”结尾。释放返回数组占用的内存是调用者的责任。如果文本缓冲区中没有行,则返回 NULL。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct textbuffer *TB;
typedef struct textbuffer {
char *texts;
TB next;
} textbuffer;
char *dumpTB (TB tb) { // my version of dumpTB
TB temp = malloc(sizeof(struct textbuffer));
temp->texts = tb->texts;
temp->next = NULL;
return (temp->texts);
}
TB newTB (char text[]){ // get the array from main function
TB newText = malloc(sizeof(struct textbuffer)); // return the node
newText->texts = text;
//strcpy(newText->texts,text);
newText->next = NULL;
return (newText);
}
void printList(TB tb){ //print entire list
TB curr = tb;
while(curr != NULL){
printf("[%s]-> ",curr->texts);
curr = curr->next;
}
printf("\n");
}
int main(int argc, char * argv[]) {
int i=0;
int j=0;
char str[MAX_TEXT];
char cpy[MAX_TEXT];
char tmp[MAX_TEXT];
TB textList = NULL;
TB list = NULL;
list = textList;
fgets(str, MAX_TEXT, stdin); // input should be like
// hello\nworld\ngood\nbye\n\0
while(str[i] != '\0') {
if(str[i] == '\n') {
cpy[i] = '\0';
strcpy(tmp,cpy);
textList = newTB(tmp);
list = textList;
textList->texts = dumpTB(textList);
//TB newList = malloc(sizeof(struct textbuffer));
//list = textList;
// newList->texts = textList->texts;
textList = textList->next;
j=0;
}
cpy[j++] = str[i++];
}
printList(list);
return 0;
}
【问题讨论】:
-
你想要
cpy[j] = '\0' -
'\n'是换行符,而您的输入是两个字符,一个反斜杠 \ 和字母“n”,因此您的匹配永远不会成功。即使您确实输入了真正的换行符,您的程序也不会执行您想要的操作,因为fgets将在第一个换行符处停止读取,并产生“hello”。 -
我都试过了,但还是不行..所以我应该改变哪一部分??
-
fgets 不在循环中。像这样将输入字符串作为 'hello\nworld\ngood\nbye\n\0' 在循环之外。
-
你需要一个作品可以将\和n解释为'\n' if it.
标签: c string linked-list