2016-10-18 22 views
1

空白で区切られた数字を含む文字列が得られました。数字は、1桁、2桁、または多分もっと多くの桁にすることができます。例を確認してください。文字列をCのINT配列に分割する

"* SEARCH 2 4 5 12 34 123 207" 

私は、文字列が(それが含まれているどのように多くの数字)であるので、私はきちんと配列を開始カントどのくらいか分かりません。結果は次のようになります。

array = {2,4,5,12,34,123,207} 

これを実行する方法はありますか?

+1

あなたが試したことを示してください。基本的には文字列を解析する必要があります。['strtol'](https://www.google.ch/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=strtol)関数が役立ちます。また、['malloc'](http://www.cplusplus.com/reference/cstdlib/malloc/)と' 'realloc'(http://www.cplusplus.com/reference/cstdlib/realloc/)を見てください。 ) 機能。 –

+1

['strtok'](http://en.cppreference.com/w/c/string/byte/strtok)関数も便利です。 –

+1

動的配列が必要です:https://en.wikipedia.org/wiki/C_dynamic_memory_allocation – Gravell

答えて

-2

各文字を読んで、> 48(Ascii of 0)と= 57(Ascii of 9)の範囲内にあるかどうかを確認できます。その場合には)そうでなければ、あなたが一時的な文字列にコピーし、(atoi関数などの関数を使ってint型に変換することができ、配列の中にこのような

#include <stdio.h> 

int main(int argc, char *argv[]) 
{ 
    int j=0,k,res; 
    char buff[10]; 
    while(str[j]) 
    { 
     if((str[j]>='0')&&(str[j]<='9')) 
     { 
      k=0; 
      while((str[j]!=' ')&&(str[j]!='\0')) 
      { 
       buff[k]=str[j++]; 
       k++; 
      } 
      buff[k]=0; 
      res=atoi(buff); 
      //Store this result to an array 
     } 
     j++; 
    } 
    return 0; 
} 
+3

回答のコードは、少なくとも適切にフォーマットされている必要があります。正してください。私は答えを下落させていない(まだ)。あなたのコードは不完全で、コンパイルされません。文字列に11個の数字が含まれているとどうなりますか? –

+0

@MichaelWalz残っているのは、これらの値を動的配列に格納することです。 数字が11桁の場合は、それ以上整数ではありません(範囲を超えています)。 あなたが話しているフォーマットの種類を知ることができますか? – Anjaneyulu

+0

私はどのような書式設定を知っていますか? ここは新しいです! – Anjaneyulu

2

それらを読んでいる場合:

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

int main(void){ 
    char *input = "* SEARCH 2 4 5 12 34 123 207"; 
    int len = 0; 

    sscanf(input, "%*[^0-9]%n", &len);//count not-digits(The Number isn't negative) 

    char *p = input + len; 
    char *start = p; 
    int v, n = 0; 
    while(1 == sscanf(p, "%d%n", &v, &len)){ 
     ++n;//count elements 
     p += len; 
    } 
    int array[n];//or allocate by malloc(and free) 
    char *endp = NULL; 
    int i; 
    for(i = 0; i < n; ++i){ 
     array[i] = strtol(start, &endp, 10); 
     start = endp + 1; 
    } 
    //check print 
    for(i = 0; i < n; ++i) 
     printf("%d ", array[i]); 
    puts(""); 
    return 0; 
} 
+0

@chux非数値の文字は、サンプルの先頭にあるとみなされます。しかし、あなたのアドバイスに従って変更すること。ありがとう。 – BLUEPIXY

0

をあなたはこれを試すことができますアプローチ。一時バッファを使用して、処理中の現在の整数を保持します。また、動的配列を使用して処理する文字列の長さを処理し、必要に応じて展開します。使用していますがstrtokこの状況で良いでしょう。

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

int 
main(int argc, char *argv[]) { 
    char message[] = "* SEARCH 2 4 5 12 34 123 207"; 
    char *buffer = NULL; 
    int *integers = NULL; 

    int buff_size = 1, buff_len = 0; 
    int int_size = 1, int_len = 0; 
    int ch, messlen, i, first_int = 0; 

    /* creating space for dynamic arrays */ 
    buffer = malloc((buff_size+1) * sizeof(*buffer)); 
    integers = malloc(int_size * sizeof(*integers)); 

    /* Checking if mallocs were successful */ 
    if (buffer == NULL || integers == NULL) { 
     fprintf(stderr, "Malloc problem, please check\n"); 
     exit(EXIT_FAILURE); 
    } 

    messlen = strlen(message); 

    /* going over each character in string */ 
    for (ch = 0; ch < messlen; ch++) { 

     /* checking for first digit that is read */ 
     if (isdigit(message[ch])) { 
      first_int = 1; 

      /* found, but is there space available? */ 
      if (buff_size == buff_len) { 
       buff_size++; 
       buffer = realloc(buffer, (2*buff_size) * sizeof(*buffer)); 
      } 
      buffer[buff_len++] = message[ch]; 
      buffer[buff_len] = '\0'; 
     } 

     /* checking for first space after first integer read */ 
     if (isspace(message[ch]) && first_int == 1) { 
      if (int_size == int_len) { 
       int_size++; 
       integers = realloc(integers, (2*int_size) * sizeof(*integers)); 
      } 
      integers[int_len] = atoi(buffer); 
      int_len++; 

      /* reset for next integer */ 
      buff_size = 1; 
      buff_len = 0; 
      first_int = 0; 
     } 

     /* for last integer found */ 
     if (isdigit(message[ch]) && ch == messlen-1) { 
      integers[int_len] = atoi(buffer); 
      int_len++; 
     } 
    } 

    printf("Your string: %s\n", message); 

    printf("\nYour integer array:\n"); 
    for (i = 0; i < int_len; i++) { 
     printf("%d ", integers[i]); 
    } 

    /* Being careful and always free at the end */ 
    /* Always a good idea */ 
    free(integers); 
    free(buffer); 

    return 0; 
} 
関連する問題