[英]Sorting strings alphabetically in C language
我創建了一個程序,要求用戶輸入一個名稱,如果他們選擇選項2並將其存儲在字符串中,如果用戶選擇選項3,那么它將顯示該名稱。 現在對於選項4,它需要按順序按字母順序排列名稱,因此當我再次選擇選項3以查看名稱時,它將按字母順序顯示它們。 我的朋友告訴我使用strcpy(),我試過但不太成功。
這是我的代碼:
//This is my code:
#include<stdio.h>
#include<string.h>
int main() {
int i = 0;
int j = 0;
while (1) {
int num;
char name[500][100],t[100];
printf("\nPress 1 to see author info\n");
printf("Press 2 to enter a name\n");
printf("Press 3 to view names\n");
printf("Press 4 to alphabatize names\n");
scanf("%d", &num);
char arr[100];
if (num == 1)
{
printf("----------------\n");
printf("name\n");
printf("132\n");
printf("----------------\n");
}
if (num == 2)
{
printf("Enter the name (no spaces)\n");
scanf("%s", arr);
strcpy(name[i], arr);
i++;
}
if (num == 3)
{
printf("\n******************************");
for (int j = 0; j<i; j++)
{
printf("\n%s\n", &name[j]);
}
printf("\n\n\n******************************");
}
if (num == 4)
{
for (i = 1; i < j; i++) {
for (j = 1; j < i; j++) {
if (strcmp(name[j - 1], name[j]) > 0) {
strcpy(t, name[j - 1]);
strcpy(name[j - 1], name[j]);
strcpy(name[j], t);
}
}
}
;
}
return(0)
system("pause");
}
如果Barmar不想讓評論成為答案:
正如所指出的:在i
你在外環用的是總的,你不能使用它,你需要一個新鮮的,說k
。 按字母順序排序給出:
int k;
if (num == 4) {
for (j = 0; j < i; j++) {
for (k = 0; k < i - j - 1; k++) {
if (strcmp(name[k], name[k + 1]) > 0) {
strcpy(t, name[k]);
strcpy(name[k], name[k+1]);
strcpy(name[k+1], t);
}
}
}
}
我使用了一種稍微不同的方法進行冒泡排序,這就是我學習它的方法,希望你能理解它。
注意:復制完整的字符串不是最好的方法,但我不知道你是否已經學習了指針。
修改了你的代碼。 看起來對於這種情況,開關盒會更漂亮,你的循環永遠不會結束。 用@Barmar說的,你的循環return 0;
因此過早終止。 我已經更改了代碼的一些元素,因為其中一些元素適合不受干擾等。
除了開關案例的實施
- char數組的名稱和位置。 對於t [100],由於它是一個臨時對象/變量,我將其移動到
case (4)
(參見修改后的代碼中的第63行),以便按需創建,而不是提前創建的原始代碼。 將t重命名為temp。 至於arr,我已將其重命名為更清晰(nameIn表示名稱輸入)- num的名字 為了更容易理解代碼,我將其設置為nameCount,以指示名稱的數量或大小。
- 重組循環,以便它們的計數器不會以任何方式修改nameCount,只將其用作比較的平均值。
- 添加了循環終止的退出條件,-1以及提供無效輸入時的默認情況。 (不檢查字母輸入)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int nameCount = 0, num = 0;
while (num != -1)
{
char nameIn[100];
char name[100][100];
printf("\nPress 1 to see author info\n");
printf("Press 2 to enter a name\n");
printf("Press 3 to view names\n");
printf("Press 4 to alphabatize names\n");
scanf("%d", &num);
switch(num)
{
case (1):
{
printf("----------------\n");
printf("name\n");
printf("132\n");
printf("----------------\n");
break;
}
case (2):
{
printf("Enter the name (no spaces)\n");
scanf("%s", nameIn);
strcpy(name[nameCount], nameIn);
nameCount++;
break;
}
case (3):
{
printf("\n******************************");
for (int ctr = 0; ctr<nameCount; ctr++)
{
printf("\n%s\n", &name[ctr]);
}
printf("\n\n\n******************************");
break;
}
case (4):
{
for (int ctrLv1 = 0; ctrLv1 < nameCount-1; ctrLv1++)
{
printf("\nhere here");
for (int ctrLv2 = ctrLv1+1; ctrLv2 < nameCount; ctrLv2++)
{
printf("\nif fail?\n\n\n");
if (strcmp(name[ctrLv1], name[ctrLv2]) > 0)
{
char temp[100] = "";
strcpy(temp, name[ctrLv1]);
strcpy(name[ctrLv1], name[ctrLv2]);
strcpy(name[ctrLv2], temp);
}
}
}
break;
}
case (-1):
{
printf("\nExiting...\n");
break;
}
default:
{
printf("Invalid input");
break;
}
}
}
return(0);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.