2016-07-25 104 views
0

我写了一个程序,接受一个名为source的字符串。然后它可以接受另一个字符串insertion。然后用户选择source中的哪个position他们想要插入insertion。然后打印结果。为什么我不能输入两个空格字符串?

当我输入字符串如"the young son"时,程序运行的很好。我可以简单地在scanf函数中为source[]添加[^\n]

如果我再输入"per"使用常规scanf,并选择将其放置在position = 10,然后我得到了一个很好打印出来"the young person"

但是,如果我选择有一个空间的插入,如"fat "投入"the cat",希望完成"the fat cat",那么它会如果我一起正常进行,并帮它忽略的空间。

,或者,如果我添加[^\n]scanf插入过,它实际上决定取消了以前的,它甚至没有让我输入一个insertion,它只是使用"the""cat",并假定它们是独立的。

有没有办法解决这个问题?

请记住,这是一个学习练习(我已经通过了这个标准,这只是我的挑剔),其中我不允许使用printfscanf以外的任何库函数。我也无法使用任何指针语法,因为它尚未被覆盖,我不想跳过。

谢谢!

这里是我下面的代码:

#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; 
} 
+1

' “%[^ \ n]的”' - >' “%39 [^ \ n]的%* C”' – BLUEPIXY

+1

忘记你听说过scanf'的',并以'fgets'代替(或者,如果你有,getline);这是前两次对'scanf'的调用的替代方案;第三,你还需要'strtol'。 – zwol

回答

0

考虑使用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; 
} 
0

的基本问题是,scanf("%[^\n]", ...最多读取但不包括换行符。所以换行符仍然在等待输入,以便以后的scanf调用读取。这意味着第二个电话将不会读取任何内容(下一个字符仍然是换行符,因此会在发生故障时立即停止)。

因此,您需要阅读并放弃换行符。 scanf("%*c");scanf("%*1[\n]");可以做到这一点。

另一个问题是,您没有检查scanf的返回值,也没有限制输入以避免溢出字符串。您可以改为执行如下操作:

if (scanf("%39[^\n]%*1[\n]", source) < 1) { 
    printf("There was a problem!\n"); 
    exit(1); } 
相关问题