小编Ngu*_*Hậu的帖子

“获取;”之间的区别 和“scanf(”%s”, s);” 在C中

我正在编写一个程序,允许用户输入五个名称并按字母顺序对名称进行排序,两个相邻名称由换行符分隔。这是我的代码:

void sortWords(char s[][100], int n){
    int i, j;
    char *str;
    for(i = 0; i < n-1; i++){
        for(j = n- 1; j > i; j--){
            if(strcmp(s[j], s[j-1]) == -1){
                strcpy(str, s[j]);
                strcpy(s[j], s[j-1]);
                strcpy(s[j-1], str);
            }
        }
    }
}
int main(){
    char s[5][100];
    int i;
    for(i = 0; i < 5; i++){
        fflush(stdin);
        //gets(s[i]);      // when I use this statement, my program doesn't work
        scanf("%s", s[i]);
    }
    sortWords(s, 5);
    for(i = 0; i < 5; i++){
        printf("%s ", s[i]); …
Run Code Online (Sandbox Code Playgroud)

c string gets scanf

1
推荐指数
1
解决办法
655
查看次数

标签 统计

c ×1

gets ×1

scanf ×1

string ×1