c - 为什么我不能输入两个带空格的字符串?

标签 c arrays string

我编写了一个程序,它接受名为 source 的字符串。然后它可以接受另一个名为 insertion 的字符串。然后,用户在中选择他们想要插入插入位置。然后它打印回结果。

当我输入诸如“the young son”之类的字符串时,程序运行得非常好。我只需在 source[]scanf 函数中添加 [^\n] 即可。

如果我然后使用常规 scanf 输入 "per" 并选择将其放置在 position = 10 处,那么我会得到一个很好的结果打印出“年轻人”

但是,如果我选择插入一个空格,例如将 "fat " 放入 "the cat" 中,希望得到一个完整的 “肥猫”,那么如果我照常进行并将其捆绑在一起,它将忽略该空格。

或者,如果我将 [^\n] 添加到 scanf 中进行插入,它实际上决定取消前一个,并且它甚至不让我输入一个插入,它只使用“the”“cat”并假设它们是分开的。

有什么办法可以解决这个问题吗?

请记住,这是一个学习练习(我已经通过了标准,这只是我挑剔),其中除了 printf 之外,我不允许使用任何库函数> 和 scanf。我也无法使用任何指针语法,因为它尚未涵盖,而且我不想跳过。

谢谢!

下面是我的代码:

#include <stdio.h>

void insertString(char source[], char insertion[], int position);
int stringLength(const char string[]);

int main(void)
{
    int position;
    char source[40]; 
    char insertion[40];

    printf("What's your first string?\n");
    scanf("%[^\n]s", source);

    printf("What do you want to insert?\n");
    scanf("%[^\n]s", insertion);

    printf("Where do you wanna put it?\n");
    scanf("%i", &position); // 

    // call function
    insertString(source, insertion, position);

    // print out result, stored in source
    printf("Here's the result: %s\n", source);

    return 0;
}

void insertString(char source[], char insertion[], int position)
{
    int i, j;

    // Find length of string
    int lengthBig = stringLength(source);
    int lengthSmall = stringLength(insertion);
    int lengthTotal = (lengthBig + lengthSmall) -1;

    // move up source characters after position
    for(i = lengthBig - 1; i >= position; i--)
    {
        source[i + (lengthSmall) ] = source[i];
    }

    // move in insertion characters 
    for(j = lengthSmall-1; j >= 0; j--)
    {
        source[position + j] = insertion[j];
    }

    // add null character 
    source[lengthTotal + 1] = '\0';
}

int stringLength(const char string[])
{
    int count =0;

    while(string[count] != '\0')
        count++;

    return count;
}

最佳答案

考虑使用 fgets 进行输入并使用 sscanf 解析整数。需要包含源数组的大小以避免写入太多字符。一对指针可用于迭代 insertString 中的数组。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define SIZE 256

void insertString(char source[], int size, char insertion[], int position);
int stringLength(const char string[]);

int main(void)
{
    int position;
    int result = 0;
    char source[SIZE];
    char insertion[SIZE];
    char input[SIZE];

    printf("What's your first string?\n");
    if ( ( fgets( source, sizeof ( source), stdin)) == NULL) {
        printf ( "could not get input\n");
        return 1;
    }
    source[strcspn ( source, "\n")] = '\0';//remove newline

    printf("What do you want to insert?\n");
    if ( ( fgets( insertion, sizeof ( insertion), stdin)) == NULL) {
        printf ( "could not get input\n");
        return 1;
    }
    insertion[strcspn ( insertion, "\n")] = '\0';//remove newline

    do {
        printf("Where do you want to put it?\n");
        if ( ( fgets( input, sizeof ( input), stdin)) == NULL) {
            printf ( "could not get input\n");
            return 1;
        }
        result = sscanf(input, "%d", &position);
    } while ( result != 1 || position < 0);//loop on bad input

    insertString(source, sizeof ( source), insertion, position);

    printf("Here's the result: %s\n", source);
    return 0;
}

void insertString(char source[], int size, char insertion[], int position)
{
    char *to = NULL, *from =NULL;

    // Find length of string
    int lengthBig = stringLength(source);
    int lengthSmall = stringLength(insertion);
    int lengthTotal = (lengthBig + lengthSmall);

    if ( lengthTotal >= size) {
        printf ( "not enough space\n");
        return;
    }

    if ( position > lengthBig) {
        position = lengthBig;
    }

    //shift to make room for insertion
    to = &source[lengthTotal + 1];
    from = &source[lengthBig + 1];

    while(from != &source[position])//loop back to source[position]
    {
        *to = *from;
        to--;
        from--;
    }
    *to = *from;

    //put insertion into source
    to = &source[position];
    from = insertion;

    while(*from)//loop until '\0'
    {
        *to = *from;
        to++;
        from++;
    }
}

int stringLength(const char string[])
{
    int count =0;
    while(*string) {
        count++;
        string++;
    }
    return count;
}

关于c - 为什么我不能输入两个带空格的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38578982/

相关文章:

c - 如何在go中将指向 slice 的指针传递给C函数

arrays - 二元运算符 '+' 不能应用于两个 'Set<String>' 操作数错误

c - 无法使用 'strcpy()'函数。即使使用 #include <string.h> 后也会生成错误

php - 算法 - 在多个数组中均匀分布项目(如果 n 个项目 < n 个数组)

php - 从 MySQL 结果创建层次数组

c - 如何在C中将 "\003"转换为int

C 线性搜索无法使用 strcmp 比较两个字符串,编译正常

c - 是否允许编译器向标准头文件添加函数?

c - 在c中生成随机矩阵

c - C 中的 malloc 函数