C 为什么fgets()不';不在第一个元素后读取吗?
我正试图在我研究的书中完成这个编程项目: 编写一个程序,对用户输入的一系列单词进行排序: 输入单词:foo 输入单词:bar 输入单词:baz 输入单词:quux 输入单词: 按排序顺序:bar baz foo quux 假设每个单词的长度不超过20个字符。当用户输入空单词时停止阅读(即,按Enter键而不输入单词)。使用指针数组跟踪字符串,将每个单词存储在动态定位的字符串中 读取完所有单词后,对数组进行排序(使用任何排序技术),然后使用循环按排序顺序打印单词 这就是我正在尝试的:C 为什么fgets()不';不在第一个元素后读取吗?,c,string,gcc,C,String,Gcc,我正试图在我研究的书中完成这个编程项目: 编写一个程序,对用户输入的一系列单词进行排序: 输入单词:foo 输入单词:bar 输入单词:baz 输入单词:quux 输入单词: 按排序顺序:bar baz foo quux 假设每个单词的长度不超过20个字符。当用户输入空单词时停止阅读(即,按Enter键而不输入单词)。使用指针数组跟踪字符串,将每个单词存储在动态定位的字符串中 读取完所有单词后,对数组进行排序(使用任何排序技术),然后使用循环按排序顺序打印单词 这就是我正在尝试的: #inclu
#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 20
int compare ( const void * p, const void * q);
int main (void){
int n;
printf("How many words do you want to write");
scanf("%d", &n);
fflush(stdin);
char * word[n];
for( int i = 0; i < n; i++){
fprintf(stdout , "Waiting for the word:");
if(fgets(word[i] , MAX_LENGTH +1 , stdin) == "\n")
break;
}
qsort((void *)word,n,sizeof(int),compare);
printf("Ordered list is:\n\n");
for( int i = 0; i < n; i++){
fprintf(stdout , "\t %s", word[i]);
}
}
int compare (const void * p, const void * q){
return strcmp( * (char**) p , * (char **) q);
}
还有一个恼人的错误让我无法完成练习,让我对这本充满挑战的书的其余部分感到“冷静”:
我做了一个小试验(代码肯定可以优化)
请
#包括
以获取qsort()
的函数声明。投票结束只需简单的打字。包含函数所在的标题,就这么简单。manqsort
将显示所需的#Include
s。如果你不使用UNIX/Linux系统,你可以在你最喜欢的搜索引擎中输入。另外,请注意If(fgets(word[i],MAX_LENGTH+1,stdin)=“\n”)中的错误。fgets()
的返回值永远不会与字符串文本相同。请看@Lundin,事情并没有那么简单。更多:没有为指针数组的元素分配内存char*word[n]代码>。遗憾的是,代码有多个错误,而不仅仅是一个简单的打字错误。这在linux gcc编译器上工作。。。如果它在windows上不工作,请发布输出。。。
C:\Users\Lenovo\Desktop\ogrenme\ch17>sorting
How many words do you want to write4
Waiting for the word:asd
C:\Users\Lenovo\Desktop\ogrenme\ch17>
sorting.c:20:5: warning: implicit declaration of function 'qsort' [-Wimplicit-function-declaration]
qsort((void *)word,n,sizeof(int),compare);
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (void) {
int n = 0;
char **string_list;
char *tmp;
//allocate memory for the first element
string_list = (char**)malloc(sizeof(char*)*1);
//infinite loop
while ( 1 ) {
//allocate size of each element to a max of 20 chars
string_list[n] = (char*)malloc(sizeof(char)*20);
printf("Enter word : ");
fgets (string_list[n], 20, stdin);
//remove trailing return carriage
string_list[n][strlen(string_list[n]) - 1] = '\0';
//break the loop here if user enters empty string
if (strlen(string_list[n]) < 1) {
break;
}
//add counter
n++;
//add memory to contain another element
string_list = realloc(string_list, sizeof(char*)*(n+1));
}
printf("\n\nInitial List is:\n");
for (int i=0 ; i<n-1 ; i++) {
printf("%s - ", string_list[i]);
}
printf("%s\n\n", string_list[n-1]);
//sorting the list
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
if (strcmp(string_list[i], string_list[j]) < 0) {
tmp = string_list[i];
string_list[i] = string_list[j];
string_list[j] = tmp;
}
}
}
printf("Sorted List is:\n");
for (int i=0 ; i<n-1 ; i++) {
printf("%s - ", string_list[i], strlen(string_list[i]));
}
printf("%s\n\n", string_list[n-1], strlen(string_list[n-1]));
}
$ ./sort
Enter word : foo
Enter word : bar
Enter word : baz
Enter word : quux
Enter word :
Initial List is:
foo - bar - baz - quux
Sorted List is:
bar - baz - foo - quux