C:字符串数组——只能输入n-1个字符串,输入的大小为n。

时间:2022-04-18 21:46:54

I have to sort strings in a lexicographical order using the Bubble Sort technique without using any library functions. I have written the following code which is working fine in sorting the strings.

But the problem is that if I give n as the input (say n = 4), I can enter only n-1 strings (only 3 strings). The problem can be solved by running the for loops from 0 to n, but that isn't a logical solution.

What am I doing wrong here?

我必须使用气泡排序技术在字典顺序中对字符串排序,而不需要使用任何库函数。我已经编写了下面的代码,它在对字符串进行排序时运行良好。但问题是,如果我把n作为输入(比如n = 4),我只能输入n-1个字符串(只有3个字符串)。可以通过运行for循环从0到n来解决这个问题,但这不是一个逻辑解决方案。我在这里做错了什么?

#include <stdio.h>
#include <string.h>
#include <malloc.h>
void swap(int indx[], int j)
{
    int temp;
    temp = indx[j];
    indx[j] = indx[j+1];
    indx[j+1] = temp;
}
void sort(char **str, int indx[], int n)
{
    int i, j, k;
    for(i=0; i<n; i++)
    {
        for(j=0; j<n-i-1; j++)
        {
            k = 0;
            while(str[j][k] != '\0')
            {
                if((str[indx[j]][k]) > (str[indx[j+1]][k]))
                {
                    swap(indx, j);
                    break;
                }
                else if((str[indx[j]][k]) < (str[indx[j+1]][k]))
                    break;
                else
                    k++;
            }
        }
    }
}
void display(char **str, int indx[], int n)
{
    int i;
    printf("Sorted strings : ");
    for(i=0; i<n; i++)
        printf("%s\n", str[indx[i]]);
}
int main(void)
{
    char **str;
    int n, i, j, *indx;
    printf("Enter no. of strings : ");
    scanf("%d", &n);
    str = (char **) malloc (n * (sizeof(char *)));
    indx = (int *) malloc (n * sizeof(int));
    for(i=0; i<n; i++)
        str[i] = (char *)malloc(10 * sizeof(char));
    printf("Enter the strings : ");
    for(i=0; i<n; i++)
    {
        gets(str[i]);
        indx[i] = i;
    }
    sort(str, indx, n);
    display(str, indx, n);
}

3 个解决方案

#1


3  

The problem is your use of scanf(). When you do scanf("%d", &n), the scanf() function reads input until it finds an integer, and puts the value into n. However, when you entered that integer, you didn't just type '4', you typed '4' and pressed Enter. And the newline is still in the input buffer. The gets() function, on the other hand, reads input up to and including the first newline, and the newline character is discarded. So when you're reading the input strings, the gets call to gets() reads the newline, and returns immediately. And then, the first string that you enter is read by the second call to gets()...

问题是使用scanf()。当您使用scanf(“%d”,&n)时,scanf()函数读取输入,直到它找到一个整数,并将值放入n.然而,当您输入该整数时,您不只是键入“4”,您输入“4”并按下Enter。新行仍然在输入缓冲区中。另一方面,get()函数读取到的输入,包括第一个换行,换行符被丢弃。因此,当您读取输入字符串时,会调用get()读取换行符,并立即返回。然后,输入的第一个字符串被第二次调用读取()…

Incidentally, The gets() function should never, ever, under any circumstances, ever be used for real programs, because it doesn't allow you to limit input. Better would be to use fgets(). fgets(str[i], BUFFERSIZE-1, stdin).

顺便说一下,get()函数在任何情况下都不应该被用于真正的程序,因为它不允许您限制输入。更好的方法是使用fgets()。fgets(str[我],BUFFERSIZE-1 stdin)。

#2


0  

int main(void)
{
char **str;
int n=4, i, j, *indx;
printf("Enter no. of strings : ");
//scanf("%d", &n);
str = (char **) malloc (n * (sizeof(char *)));
indx = (int *) malloc (n * sizeof(int));
for(i=0; i<n; i++)
    str[i] = (char *)malloc(10 * sizeof(char));
printf("Enter the strings : ");
for(i=0; i<n; i++)
{
    gets(str[i]);
    indx[i] = i;
}
sort(str, indx, n);
display(str, indx, n);
}

//if i comment out scanf and give int the value it works fine // so the problem is use of fgets just after scanf as scanf leave a newline character in the buffer// so consume it before using fgets

//如果我对scanf进行注释,并给出它工作的值,那么问题就是在scanf之后使用fgets,因为scanf在缓冲区中保留了一个换行符,所以在使用fgets之前使用它。

#3


0  

Try this at the line where you have to input the string. Instead of:

在需要输入字符串的地方试试这个。而不是:

gets(str[i]);

type:

类型:

scanf("%s",str[i]);

#1


3  

The problem is your use of scanf(). When you do scanf("%d", &n), the scanf() function reads input until it finds an integer, and puts the value into n. However, when you entered that integer, you didn't just type '4', you typed '4' and pressed Enter. And the newline is still in the input buffer. The gets() function, on the other hand, reads input up to and including the first newline, and the newline character is discarded. So when you're reading the input strings, the gets call to gets() reads the newline, and returns immediately. And then, the first string that you enter is read by the second call to gets()...

问题是使用scanf()。当您使用scanf(“%d”,&n)时,scanf()函数读取输入,直到它找到一个整数,并将值放入n.然而,当您输入该整数时,您不只是键入“4”,您输入“4”并按下Enter。新行仍然在输入缓冲区中。另一方面,get()函数读取到的输入,包括第一个换行,换行符被丢弃。因此,当您读取输入字符串时,会调用get()读取换行符,并立即返回。然后,输入的第一个字符串被第二次调用读取()…

Incidentally, The gets() function should never, ever, under any circumstances, ever be used for real programs, because it doesn't allow you to limit input. Better would be to use fgets(). fgets(str[i], BUFFERSIZE-1, stdin).

顺便说一下,get()函数在任何情况下都不应该被用于真正的程序,因为它不允许您限制输入。更好的方法是使用fgets()。fgets(str[我],BUFFERSIZE-1 stdin)。

#2


0  

int main(void)
{
char **str;
int n=4, i, j, *indx;
printf("Enter no. of strings : ");
//scanf("%d", &n);
str = (char **) malloc (n * (sizeof(char *)));
indx = (int *) malloc (n * sizeof(int));
for(i=0; i<n; i++)
    str[i] = (char *)malloc(10 * sizeof(char));
printf("Enter the strings : ");
for(i=0; i<n; i++)
{
    gets(str[i]);
    indx[i] = i;
}
sort(str, indx, n);
display(str, indx, n);
}

//if i comment out scanf and give int the value it works fine // so the problem is use of fgets just after scanf as scanf leave a newline character in the buffer// so consume it before using fgets

//如果我对scanf进行注释,并给出它工作的值,那么问题就是在scanf之后使用fgets,因为scanf在缓冲区中保留了一个换行符,所以在使用fgets之前使用它。

#3


0  

Try this at the line where you have to input the string. Instead of:

在需要输入字符串的地方试试这个。而不是:

gets(str[i]);

type:

类型:

scanf("%s",str[i]);