2016-07-25 7 views
0

sourceという文字列を受け取るプログラムを作成しました。その後、insertionという別の文字列を受け入れることができます。ユーザは、sourceinsertionを挿入することを選択します。その後、結果を印刷します。2つの文字列をスペースで入力できないのはなぜですか?

"the young son"のような文字列を入力すると、プログラムは正常に実行されます。 source[]scanfの機能の間に、単に[^\n]を追加することができます。

I、入力"per"が定期的にscanfを使用してposition = 10でそれを置くことを選択した場合、その後、私が手にうまく"the young person"をプリントアウトします。私が完了し"the fat cat"を期待して、"the cat"に入れるためにこのような"fat "として、スペースで挿入を持っていることを選択した場合、私は通常と一緒にたくさんそれをように進めた場合

しかし、それはスペースを無視します。

OR、私はあまりにも挿入するためscanf[^\n]を追加する場合、それは実際に以前を相殺することを決定し、それも私が入力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]は、S"' - > ' "%39 [^ \ nを]%※C"' – BLUEPIXY

+1

あなたはscanf' 'のことを聞いたことは忘れて、代わりに' fgets'を使用(もしあなたがそれを持っていれば、 'getline');これは 'scanf'への最初の2回の呼び出しのための置き換えです。三番目には '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呼び出しの入力を待っています。これは、2番目の呼び出しが何も読み取らないことを意味します(次の文字はまだ改行されているため、失敗するとすぐに終了します)。

改行を読み、破棄する必要があります。 scanf("%*c");またはscanf("%*1[\n]");とすることができます。

もう1つの問題は、scanfの戻り値をチェックしておらず、文字列のオーバーフローを避けるために入力を制限していないことです。

if (scanf("%39[^\n]%*1[\n]", source) < 1) { 
    printf("There was a problem!\n"); 
    exit(1); } 
関連する問題