user-0 提问: C语言字符串的输入，排序和输出，程序错误，花了我很长时间，谁能帮我调试下，找出错误原因。

回复:
user: user-0
content: 最终还是自己解决问题，也不会在发帖请人帮忙解决具体问题了，但希望人们能教给我方法和知识。

user: user-0
content: //第11章第10题                                      //输入10个字符串，按不同要求排序输出  #include <stdio.h> #include <string.h> #define LIM 10 #define SIZE 81 void str_oriout (char *fstr[],int num); void str_ascout (char *fstr[],int num);  void str_lenout (char *fstr[],int num); void str_wordout (char *fstr[],int num);   int main (void) { 	char str[LIM][SIZE];                       //char *str[LIM] ,输入存在问题，一次只能输入一行      char * ptstr[LIM];                         //注意声明！！      char ch = 'N'; 	int i,j; 	i = 0; 	j = 0; 	int is_EOF; 	 	printf("Please enter 10 strings\n"); 	while (ch != 'Y') 	{ 		    ch = getchar(); 	        while (i < LIM && ch != EOF)   			      //while (ch = getchar() && i < LIM && ch != EOF)出错，为何？？  			{ 		         if (ch == '\n') 				    { 					 str[i][j] = '\0'; 					 ptstr[i] = str[i]; 					 i++;    				     j = 0; 					} 			     else 				    { 					 str[i][j] = ch; 					 j++; 					}  					 				 ch = getchar();	 			}     			         str_oriout(ptstr,i); 		str_ascout(ptstr,i); 		str_lenout(ptstr,i); 		str_wordout(ptstr,i); 		         i = 0;         j = 0; 		printf("Enter 'Y' to get out!\n"); 		ch = getchar(); 		getchar(); 	}      	return 0;   }    void str_oriout (char *fstr[],int num)        //输出初始字符串  { 	printf("******************1\n");  	int ct = 0; 	for (ct = 0;ct < num;ct++) 		puts(fstr[ct]); }  void str_ascout (char *fstr[],int num)        //按首字母ASCII值的顺序输出字符串  { 	printf("******************2\n");  	char *temp; 	int top,seek; 	for (top = 0;top < num - 1;top++) 	    for (seek = top + 1;seek < num;seek++) 	        if (strcmp(fstr[top],fstr[seek]) > 0) 	        { 	        	temp = fstr[seek]; 	        	fstr[seek] = fstr[top]; 	        	fstr[top] = temp; 			} 	for (top = 0;top < num;top++) 		puts(fstr[top]); }  void str_lenout (char *fstr[],int num)         //按数组长度排序  { 	printf("*******************3\n");  	char *temp; 	int top,seek; 	 	for (top = 0; top < num-1; top++)         for (seek = top + 1; seek < num; seek++)              if (strlen(fstr[top]) < strlen(fstr[seek]))                  {                  	temp = fstr[top];                  	fstr[top] = fstr[seek];                  	fstr[seek] = temp;  				 }   	 	for (top = 0;top < num;top++) 		puts(fstr[top]);			 	 }  void str_wordout (char *fstr[],int num)          //按第一个单词的长度排序  { 	printf("**********************4\n");  	char *temp; 	int top,seek; 	 	for (top = 0; top < num-1; top++)         for (seek = top + 1; seek < num; seek++)             {             	int i,j;             	int count1,count2;             	i = j = 0; 				count1 = count2 = 1;             	             	while (fstr[top][i] != '\0')                        {count1++;                         i++;}                 while (fstr[seek][j] != '\0') 				       {count2++; 				        j++;} 			    if (count1 < count2) 				   { 				   	temp = fstr[top]; 				   	fstr[top] = fstr[seek]; 				   	fstr[seek] = temp;				    	 				   }	         			} 			     for (top = 0;top < num;top++) 		 puts(fstr[top]);			 	 }

user: user-0
content: 何不改成getchar（）输入呢？

user: user-1
content: 修改如下：  //第11章第10题             //C Primer Plus                                  //输入10个字符串，按不同要求排序输出  #include <stdio.h> #include <string.h> #define LIM 10 #define SIZE 81 void str_oriout(char *fstr[], int num); void str_ascout(char *fstr[], int num); void str_lenout(char *fstr[], int num); void str_wordout(char *fstr[], int num); int main(void) { 	char str[LIM][SIZE];                //char *str[LIM] ,输入存在问题，一次只能输入一行  	char * ptstr[LIM];                  //注意声明！！  	char ch = 'N'; 	int i, j; 	i = 0; 	j = 0; 	int is_EOF; 	printf("Please enter 10 strings\n"); 	while (ch != 'Y') 	{ 		while (i < LIM)             //输入10个字符串或遇到EOF结束  		{ 			gets(str[i]);  			ptstr[i] = str[i];  			for (j = 0; j < strlen(str[i]); j++)  //遇到EOF停止   			{ 				if (ptstr[i][j] == EOF) 				{ 					is_EOF = 1; 					break;			   	   //break    				} 			} 			if (is_EOF == 1) 				break;  			puts(str[i]); 			printf("******1\n");          //检查作用  			puts(ptstr[i]); 			printf("******2\n"); 			i++; 		} 		str_oriout(ptstr, i); 		str_ascout(ptstr, i); 		str_lenout(ptstr, i); 		str_wordout(ptstr, i);  		printf("Enter 'Y' to get out!\n"); 		ch = getchar(); 		getchar(); 	}  	return 0; } void str_oriout(char *fstr[], int num)        //输出初始字符串  { 	printf("******1\n"); 	int ct = 0; 	for (ct = 0; ct < num; ct++) 		puts(fstr[ct]); } void str_ascout(char *fstr[], int num)        //按首字母ASCII值的顺序输出字符串  { 	printf("******2\n"); 	char *temp; 	int top, seek; 	for (top = 0; top < num - 1; top++) 		for (seek = top + 1; seek < num; seek++) 			if (strcmp(fstr[top], fstr[seek]) > 0) 			{ 				temp = fstr[seek]; 				fstr[seek] = fstr[top]; 				fstr[top] = temp; 			} 	for (top = 0; top < num; top++) 		puts(fstr[top]); } void str_lenout(char *fstr[], int num)         //按数组长度排序  { 	printf("******3\n"); 	char *temp; 	int top, seek; 	for (top = 0; top < num - 1; top++) 		for (seek = top + 1; seek < num; seek++) 			if (strlen(fstr[top]) < strlen(fstr[seek])) 			{ 				temp = fstr[top]; 				fstr[top] = fstr[seek]; 				fstr[seek] = temp; 			} 	for (top = 0; top < num; top++) 		puts(fstr[top]); } void str_wordout(char *fstr[], int num)          //按第一个单词的长度排序  { 	printf("******4\n"); 	char *temp; 	int top, seek;  	for (top = 0; top < num - 1; top++) 		for (seek = top + 1; seek < num; seek++) 		{ 			int i, j; 			int count1, count2; 			i = j = 0; 			count1 = count2 = 1; 			while (fstr[top][i] != '\0') 			{ 				count1++; 				i++; 			} 			while (fstr[seek][j] != '\0') 			{ 				count2++; 				j++; 			} 			if (count1 < count2) 			{ 				temp = fstr[top]; 				fstr[top] = fstr[seek]; 				fstr[seek] = temp; 			} 		} 	for (top = 0; top < num; top++) 		puts(fstr[top]); }

user: user-0
content: //第11章第10题             //C Primer Plus                                  //输入10个字符串，按不同要求排序输出  #include <stdio.h> #include <string.h> #define LIM 10 #define SIZE 81 void str_oriout (char *fstr[],int num); void str_ascout (char *fstr[],int num);  void str_lenout (char *fstr[],int num); void str_wordout (char *fstr[],int num);     int main (void) { 	char str[LIM][SIZE];                       //char *str[LIM] ,输入存在问题，一次只能输入一行      char * ptstr[LIM];                         //注意声明！！      char ch = 'N'; 	int i,j; 	i = 0; 	j = 0; 	int is_EOF; 	 	printf("Please enter 10 strings\n"); 	while (ch != 'Y') 	{ 		while(i < LIM)                         //输入10个字符串或遇到EOF结束  	    { 	    	gets(str[i]); 	    	ptstr[i] = str[i]; 	    	 		    for(j = 0;j < strlen(str[i]);j++)  //遇到EOF停止   		    { 			      if (ptstr[i][j] == EOF)         			         { 			   	      is_EOF = 1; 			   	      break;			   	   //break    			         }	 			}  	           		    i++;   		     		    if (is_EOF == 1) 	           break;    	            	        puts(str[i]);   			printf("******1\n");                    //检查作用  			puts(ptstr[i]); 			printf("******2\n");  	    } 	     		str_oriout(ptstr,i); 		str_ascout(ptstr,i); 		str_lenout(ptstr,i); 		str_wordout(ptstr,i); 	 		printf("Enter 'Y' to get out!\n"); 		ch = getchar(); 		getchar(); 		     }      	return 0;   }    void str_oriout (char *fstr[],int num)        //输出初始字符串  { 	printf("******1\n");  	int ct = 0; 	for (ct = 0;ct < num;ct++) 		puts(fstr[ct]); }  void str_ascout (char *fstr[],int num)        //按首字母ASCII值的顺序输出字符串  { 	printf("******2\n");  	char *temp; 	int top,seek; 	for (top = 0;top < num - 1;top++) 	    for (seek = top + 1;seek < num;seek++) 	        if (strcmp(fstr[top],fstr[seek]) > 0) 	        { 	        	temp = fstr[seek]; 	        	fstr[seek] = fstr[top]; 	        	fstr[top] = temp; 			} 	for (top = 0;top < num;top++) 		puts(fstr[top]); }  void str_lenout (char *fstr[],int num)         //按数组长度排序  { 	printf("******3");  	char *temp; 	int top,seek; 	 	for (top = 0; top < num-1; top++)         for (seek = top + 1; seek < num; seek++)              if (strlen(fstr[top]) < strlen(fstr[seek]))                  {                  	temp = fstr[top];                  	fstr[top] = fstr[seek];                  	fstr[seek] = temp;  				 }   	 	for (top = 0;top < num;top++) 		puts(fstr[top]);			 	 }  void str_wordout (char *fstr[],int num)          //按第一个单词的长度排序  { 	printf("******4");  	char *temp; 	int top,seek; 	 	for (top = 0; top < num-1; top++)         for (seek = top + 1; seek < num; seek++)             {             	int i,j;             	int count1,count2;             	i = j = 0; 				count1 = count2 = 1;             	             	while (fstr[top][i] != '\0')                        {count1++;                         i++;}                 while (fstr[seek][j] != '\0') 				       {count2++; 				        j++;} 			    if (count1 < count2) 				   { 				   	temp = fstr[top]; 				   	fstr[top] = fstr[seek]; 				   	fstr[seek] = temp;				    	 				   }	         			} 			     for (top = 0;top < num;top++) 		 puts(fstr[top]);			 	 }

user: user-0
content: [code=c          //C Primer Plus                                   #include <stdio.h> #include <string.h> #define LIM 10 #define SIZE 81 void str_oriout (char *fstr[],int num); void str_ascout (char *fstr[],int num);  void str_lenout (char *fstr[],int num); void str_wordout (char *fstr[],int num);     int main (void) { 	char str[LIM][SIZE];                       //char         char * ptstr[LIM];                                 char ch = 'N'; 	int i,j; 	i = 0; 	j = 0; 	int is_EOF; 	 	printf("Please enter 10 strings\n"); 	while (ch != 'Y') 	{ 		while(i < LIM)                         //                 {	     	    	gets(str[i]); 	    	 		    for(j = 0;j < strlen(str[i]);j++)  // 		    { 			      if (str[i][j] == EOF)         			         { 			   	      is_EOF = 1; 			   	      break;			   	   //break   			         }	 			}  	        ptstr[i] = str[i];   		    i++;   		     		    if (is_EOF == 1) 	           break;    	            	        puts(str[i]);                       			puts(ptstr[i]); 	    } 	     		str_oriout(ptstr,i); 		str_ascout(ptstr,i); 		str_lenout(ptstr,i); 		str_wordout(ptstr,i); 	 		printf("Enter 'Y' to get out!\n"); 		ch = getchar(); 		getchar(); 		     }      	return 0;   }    void str_oriout (char *fstr[],int num)        // { 	int ct = 0; 	for (ct = 0;ct < num;ct++) 		puts(fstr[ct]); }  void str_ascout (char *fstr[],int num)        // { 	char *temp; 	int top,seek; 	for (top = 0;top < num - 1;top++) 	    for (seek = top + 1;seek < num;seek++) 	        if (strcmp(fstr[top],fstr[seek]) > 0) 	        { 	        	temp = fstr[seek]; 	        	fstr[seek] = fstr[top]; 	        	fstr[top] = temp; 			} 	for (top = 0;top < num;top++) 		puts(fstr[top]); }  void str_lenout (char *fstr[],int num)          { 	char *temp; 	int top,seek; 	 	for (top = 0; top < num-1; top++)         for (seek = top + 1; seek < num; seek++)              if (strlen(fstr[top]) < strlen(fstr[seek]))                  {                  	temp = fstr[top];                  	fstr[top] = fstr[seek];                  	fstr[seek] = temp;  				 }   	 	for (top = 0;top < num;top++) 		puts(fstr[top]);			 	 }  void str_wordout (char *fstr[],int num)          // { 	char *temp; 	int top,seek; 	 	for (top = 0; top < num-1; top++)         for (seek = top + 1; seek < num; seek++)             {             	int i,j;             	int count1,count2;             	i = j = 0; 				count1 = count2 = 1;             	             	while (fstr[top][i] != '\0')                        {count1++;                         i++;}                 while (fstr[seek][j] != '\0') 				       {count2++; 				        j++;} 			    if (count1 < count2) 				   { 				   	temp = fstr[top]; 				   	fstr[top] = fstr[seek]; 				   	fstr[seek] = temp;				    	 				   }	         			} 			     for (top = 0;top < num;top++) 		 puts(fstr[top]);			 	 }  ][/code]

user: user-0
content: 引用 3 楼 user-2 的回复:第二个while后面少了一个"{";导致程序有错，加个“｛”吧。然后再调调看。 while(i < LIM)  后加"{".  那个是编辑的时候打掉的了的，这样的错误，编译器可以检查出。

user: user-2
content: 第二个while后面少了一个"{";导致程序有错，加个“｛”吧。然后再调调看。 while(i < LIM)  后加"{".

user: user-0
content: 大一，上学期开的C，没学到什么，于是自学C Primer Plus，到了第11章字符串和字符串函数，卡在这里了。章末的程序题花我很长时间，这道题已使我用了一天或者更久。这样学习，是不是效率太低不可取？

user: user-0
content: //°´Ê××ÖÄ¸ASCIIÖµµÄË³ÐòÊä³ö×Ö·û´®  汉字注释全成了乱码！！！！谁能解释下？