user-0 提问: Temp

回复:
user: user-0
content: 改好了~ #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <string.h>  #define Length 5  void show_s(char *s[]); void sque_s(char *s[]); void length_sque(char *s[]); void first_length(char *s[]);  int _tmain(int argc, _TCHAR* argv[]) { 	char *s[10]; 	char ch = ' '; 	printf("Please enter 5 words:\n"); 	for (size_t i = 0; i < Length; i++) 	{ 		s[i] = (char*)malloc(sizeof(char) * 20); 		gets_s(s[i], 20); 	} 	printf("Please choose the next step number: 1 show_s; 2 sque_s; 3 length_sque; 4 first_length.\n"); 	while ((ch != 'q') && (ch = getchar()) != 'q') 	{ 		switch (ch) 		{ 		case '1':show_s(s); 			break; 		case '2':sque_s(s); 			break; 		case '3':length_sque(s); 			break; 		case '4':first_length(s); 			break; 		case 'q':ch = 'q'; 			break; 		default:printf("Please enter right number: 1 show_s; 2 sque_s; 3 length_sque; 4 first_length.\n"); 			break; 		} 		getchar(); 	} 	for (size_t i = 0; i < Length; i++) 		free(s[i]); 	system("PAUSE"); 	return 0; } void show_s(char *s[]) { 	for (size_t i = 0; i < Length; i++) 	{ 		printf("%s\n", s[i]); 	} } void sque_s(char *s1[]) { 	char *s[Length]; 	for (size_t i = 0; i < Length; i++) 	{ 		s[i] = s1[i]; 	} 	char *temp; 	for (size_t i = 0; i < Length; i++) 	{ 		for (size_t j = 0; j < Length - i; j++) 		{ 			if (strcmp(s[i], s[i + j]) > 0) 			{ 				temp = s[i]; 				s[i] = s[i + j]; 				s[i + j] = temp; 			} 		} 	} 	for (size_t i = 0; i < Length; i++) 	{ 		printf("%s\n", s[i]); 	} } void length_sque(char *s1[]) { 	char *s[Length]; 	for (size_t i = 0; i < Length; i++) 	{ 		s[i] = s1[i]; 	} 	char *temp; 	for (size_t i = 0; i < Length - 1; i++) 	{ 		for (size_t j = 1; i + j < Length; j++) 		{ 			if (strlen(s[i]) > strlen(s[i + j])) 			{ 				temp = s[i]; 				s[i] = s[i + j]; 				s[i + j] = temp; 			} 		} 	} 	for (size_t i = 0; i < Length; i++) 	{ 		printf("%s\n", s[i]); 	} } void first_length(char *s1[]) { 	char **s; 	s = s1; 	int f_len = 0; 	f_len = strlen(s[0]); 	for (size_t i = 0; i < Length; i++) 	{ 		for (size_t j = 0; j < f_len; j++) 		{ 			printf("%c", s[i][j]); 		} 		printf("\n"); 	} }

user: user-1
content: #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include<string.h>  void show_s(char *s[]); void sque_s(char *s[]); void length_sque(char *s[]); void first_length(char *s[]);  int _tmain(int argc, _TCHAR* argv[]) {     char *s[10];     char ch;     printf("Please enter 10 words:\n");     for (size_t i = 0; i < 10; i++)     {         s[i] = (char *)malloc(sizeof(char) * 20);         if (!s[i])             exit(0);         gets_s(s[i],20);     }     printf("Please choose the next step number: 1 show_s; 2 sque_s; 3 length_sque; 4 first_length.\n");     //while (ch = getchar() != 'q')     while ((ch = getchar()) != 'q')     {         switch (ch)         {             case '1':show_s(s);                      break;             case '2':sque_s(s);                      break;             case '3':length_sque(s);                      break;             case '4':first_length(s);                      break;             default:                      printf("Please enter right number: 1 show_s; 2 sque_s; 3 length_sque; 4 first_length.\n");                     break;         }     }     for (size_t i = 0; i < 10; i++)         free(s[i]);     system("PAUSE");     return 0; } void show_s(char *s[]) {     for (size_t i = 0; i < 10; i++)     {         printf("%s\n", s[i]);     } } void sque_s(char *s1[]) {     char **s;     s = s1;     char *temp;     int result = 0;     for (size_t i = 0; i < 10; i++)     {         for (size_t j = 0; j < 10 - i - 1; j++)         { #if 1             if (strcmp(s[j], s[j+1]) > 0)             {                 temp = s[j];                 s[j] = s[j+1];                 s[j+1] = temp;             }  #else             if (strcmp(s[i], s[i+j]) > 0)             {                 temp = s[i + j];                 s[i + j] = s[i + j + 1];                 s[i + j + 1] = temp;             } #endif         }     }     for (size_t i = 0; i < 10; i++)     {         printf("%s\n", s[i]);     } } void length_sque(char *s1[]) {     char **s;     s = s1;     char *temp;  #if 1     for (size_t j = 0; j < 10; j++)     {         for (size_t i = 0; i < 10 - j - 1; i++)             if (strlen(s[i]) > strlen(s[i+1]))             {                 temp = s[i];                 s[i] = s[i+1];                 s[i+1] = temp;             }     }  #else     for (size_t i = 0; i < 9; i++)     {         if (strlen(s[i]) > strlen(s[i + 1]))         {             temp = s[i];             s[i] = s[i + 1];             s[i + 1] = temp;         }     } #endif     for (size_t i = 0; i < 10; i++)     {         printf("%s\n", s[i]);     } } void first_length(char *s1[]) {     char **s;     s = s1;     int length = 0;     length = strlen(s[0]);     for (size_t i = 0; i < 10; i++)     {         //strncpy(s[i], s1[i], length);     }     for (size_t i = 0; i < 10; i++)     {         printf("%s\n", s[i]);     } } 参考一下吧 注意*s[10]这是10个指针，并且是野指针。使用之前需要给其分配空间。 排序算法有问题，建议对比找一下原因。

user: user-1
content: 方法是用另一个同样大小的空间存放10个字符串。调用传参用其中一个，另一个就会保存旧的字符串。

user: user-0
content: 我修改了一下，基本功能可以实现了。可是有一个问题，这样写函数，会把原始输入的字符串组修改掉。以至于下次调用数组字符串时，是修改的值。 有什么什么好的方法，让调用过后，字符串数组不改变呢？ #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <string.h>  #define Length 5  void show_s(char *s[]); void sque_s(char *s[]); void length_sque(char *s[]); void first_length(char *s[]);  int _tmain(int argc, _TCHAR* argv[]) { 	char *s[10]; 	char ch = ' '; 	printf("Please enter 10 words:\n"); 	for (size_t i = 0; i < Length; i++) 	{ 		s[i] = (char*)malloc(sizeof(char) * 20); 		gets_s(s[i], 20); 	} 	printf("Please choose the next step number: 1 show_s; 2 sque_s; 3 length_sque; 4 first_length.\n"); 	while ((ch != 'q') && (ch = getchar()) != 'q') 	{ 		switch (ch) 		{ 		case '1':show_s(s); 			break; 		case '2':sque_s(s); 			break; 		case '3':length_sque(s); 			break; 		case '4':first_length(s); 			break; 		case 'q':ch = 'q'; 			break; 		default:printf("Please enter right number: 1 show_s; 2 sque_s; 3 length_sque; 4 first_length.\n"); 			break; 		} 		getchar(); 	} 	for (size_t i = 0; i < Length; i++) 		free(s[i]); 	system("PAUSE"); 	return 0; } void show_s(char *s[]) { 	for (size_t i = 0; i < Length; i++) 	{ 		printf("%s\n", s[i]); 	} } void sque_s(char *s1[]) { 	char **s; 	s = s1; 	char *temp; 	for (size_t i = 0; i < Length; i++) 	{ 		for (size_t j = 0; j < Length - i; j++) 		{ 			if (strcmp(s[i], s[i + j]) > 0) 			{ 				temp = s[i]; 				s[i] = s[i + j]; 				s[i + j] = temp; 			} 		} 	} 	for (size_t i = 0; i < Length; i++) 	{ 		printf("%s\n", s[i]); 	} } void length_sque(char *s1[]) { 	char **s; 	s = s1; 	char *temp; 	for (size_t i = 0; i < Length - 1; i++) 	{ 		for (size_t j = 1; i + j < Length; j++) 		{ 			if (strlen(s[i]) > strlen(s[i + j])) 			{ 				temp = s[i]; 				s[i] = s[i + j]; 				s[i + j] = temp; 			} 		}		 	} 	for (size_t i = 0; i < Length; i++) 	{ 		printf("%s\n", s[i]); 	} } void first_length(char *s1[]) { 	char **s; 	s = s1; 	int f_len = 0; 	f_len = strlen(s[0]); 	for (size_t i = 0; i < Length; i++) 	{ 		for (size_t j = 0; j < f_len; j++) 		{ 			printf("%c", s[i][j]); 		} 		printf("\n"); 	} }