我想用C编程创建一个程序,可以对单个字符串的字符进行排序,并按字母顺序对字符串列表进行排序。遗憾的是,虽然我让程序对列表进行排序,但它不会对字符串每个部分中的单个字符进行排序。这是我现在的代码:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define MAX_STRING_LENGTH 20
#define TOTAL_STRING 10
int main()
{
int wordNum,i,j;
char *pointer[MAX_STRING_LENGTH];
char p, *strlist;
printf("How many words will you enter (must be at most 21):");
scanf("%d", &wordNum);
getchar();
printf("You will print %d words\n", wordNum);
if(wordNum<=MAX_STRING_LENGTH){
for(i=0;i<wordNum;i++){
j=0;
strlist=malloc(TOTAL_STRING*sizeof(char));
p=getchar();
while(p!='\n'){
*(strlist+j)=p;
j++;
p=getchar();
}
if(p=='\n'){
strlist[j]='\0';
}
pointer[i]=strlist;
}
}
for(i=0;i<wordNum-1;i++){
for(j=i+1;j<wordNum;j++){
if(strcmp(pointer[i], pointer[j])>0){
char *temp = pointer[i];
pointer[i] = pointer[j];
pointer[j] = temp;
}
}
}
printf("Words now sorted:\n");
for(i=0;i<wordNum;i++){
printf("%s\n", pointer[i]);
}
printf("Bye!");
return 0;
}
输入:geeksforgeeks 预期结果:eeeefggkkorss
我不确定您是否意识到您的代码不包含对每个单独字符数组中的字符进行排序的函数,或者您是否正在寻求如何创建此类排序函数的帮助。无论哪种情况,以下都是代码的重构版本,其中包括字符排序函数以及代码中该函数的执行。
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define MAX_STRING_LENGTH 20
#define TOTAL_STRING 10
void sortString(char *work) /* Added character array sorting function */
{
char temp;
int sorted = 1;
while (sorted)
{
sorted = 0;
for (int i = 0; i < (strlen(work) - 1); i++)
{
if (work[i] > work[i+1])
{
sorted = 1;
temp = work[i];
work[i] = work[i + 1];
work[i + 1] = temp;
}
}
}
}
int main()
{
int wordNum,i,j;
char *pointer[MAX_STRING_LENGTH];
char p, *strlist;
printf("How many words will you enter (must be at most 21): ");
scanf("%d", &wordNum);
getchar();
printf("You will print %d word(s)\n", wordNum);
if(wordNum<=MAX_STRING_LENGTH)
{
for(i=0; i<wordNum; i++)
{
printf("Enter string: "); /* Added for user look and feel */
j=0;
strlist=malloc(TOTAL_STRING*sizeof(char));
p=getchar();
while(p!='\n')
{
*(strlist+j)=p;
j++;
p=getchar();
}
if(p=='\n')
{
strlist[j]='\0';
}
pointer[i]=strlist;
}
}
for (i = 0; i < wordNum; i++) /* Sort the characters within each entered string */
sortString(pointer[i]);
for(i=0; i<wordNum-1; i++)
{
for(j=i+1; j<wordNum; j++)
{
if(strcmp(pointer[i], pointer[j])>0)
{
char *temp = pointer[i];
pointer[i] = pointer[j];
pointer[j] = temp;
}
}
}
printf("Words now sorted:\n");
for(i=0; i<wordNum; i++)
{
printf("%s\n", pointer[i]);
}
printf("Bye!\n"); /* Added newline for aesthetics */
return 0;
}
以下是重点。
经过一些重构,以下是一些测试。第一个是您的问题中记录的字符串条目,第二个测试包含多个字符串条目。
craig@Vera:~/C_Programs/Console/Stringlist/bin/Release$ ./Stringlist
How many words will you enter (must be at most 21): 1
You will print 1 word(s)
Enter string: geeksforgeeks
Words now sorted:
eeeefggkkorss
Bye!
craig@Vera:~/C_Programs/Console/Stringlist/bin/Release$ ./Stringlist
How many words will you enter (must be at most 21): 2
You will print 2 word(s)
Enter string: greatest
Enter string: least
Words now sorted:
aeegrstt
aelst
Bye!
再次,我不确定所需的帮助类型是否涉及为什么输入的字符串没有对其字符进行排序,或者是否需要帮助来构建排序函数。无论哪种方式,请继续查看重构的代码。另外,最好深入研究一些“C”教程文献,因为它涉及字符数组和函数。