我写下面的代码,但从输出中看到一些错误。也许我犯了一个指针错误。你能帮我吗?以奇怪的方式排序的输出
未分类名称:
纽约 格鲁吉亚 波士顿
排序的名字:
Bostork 格鲁吉亚 Newyon
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 3
void sort(char x[3][100]);
void swap(char **, char **);
int i,j;
char names[SIZE][100];
char *temp;
int main(void){
//get the names of the cities
puts("Enter names of cities");
for (i = 0; i < SIZE; i++)
{
fgets(names[i], 99, stdin);
}
//print entered names
puts("\nUnsorted Names:\n");
for (i = 0; i < SIZE; i++)
{
printf("%s", names[i]);
}
sort(names);
//print sorted names
puts("\nSorted Names:\n");
for (i = 0; i < SIZE; i++)
{
printf("%s", names[i]);
}
getch();
}
//sorting function
void sort(char angut[3][100]){
for (i = 0; i < SIZE-1; i++)
{
for (j = i+1; j < SIZE; j++)
{
if (strcmp(angut[i], angut[j]) >0)
{
swap(&angut[i],&angut[j]);
}
}
}
}
//swapping function
void swap(char **first, char **second){
temp=*second;
*second=*first;
*first=temp;
}
我需要波士顿格鲁吉亚纽约作为输出。 – Lyrk
你在开玩笑还是什么?不好笑 – Lyrk