2016-07-25 78 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); } 
相關問題