2016-12-15 107 views
-2

我一直在試圖製作一個程序,用於統計輸入文本中的字符數,不包括空格。我知道我必須用二維數組來做,但是當我輸入一組單詞時,我的循環只計算第一個單詞中的字符數。如何在C中輸入二維數組?

下面是主要代碼:

char text[400][40]; 
int charNum = 0; 
int i = 0; 
int j = 0; 


printf("input string: \n"); 
scanf("%s", &text); 
while(text[i] != '\0'){ 
    while(text[i][j] != '\0'){ 
     printf("\n\nword: %s\n", text[i]); 
     printf("\n\nchar:%c\n", text[i][j]);  
     charNum = charNum + j; 
     j++; 
     printf("J= %d\n", j); 
    } 
    i++; 
} 

如何輸入二維數組,因此該循環可以讀取它的每個字符串?

+1

提示:'scanf函數( 「%S」,&text);'是outrigjht錯誤檢查數據類型 –

+0

但是,這不是每個字符串聲明爲一個char,並有%S在scanf。? –

+0

[this]的可能重複(http://stackoverflow.com/questions/8211087/how-to-fill-a-2d-array-in-c-with-user-input-values) 解決方案可以找到 –

回答

0

我看你已經聲明輸入要存儲在字符文本[400] [40];這意味着您可以存儲每個40個字符長度的400個單詞。當你使用scanf輸入你的單詞時,scanf只會將輸入直到遇到第一個空格爲止。所以使用fgets來讀取輸入的單詞。例如: text [0] =「john」; text [1] =「xyzxyz xyz」; 你將能夠使用fgets讀取兩種格式。

+0

但不是fgets僅用於從文件中獲取字符串? –

+0

「*,每個長度爲40個字符。*「,不,不完全是,只有39個'char',因爲C字符串需要多一個'char'來存儲'0'-終止。 – alk

+0

該文件也可以是標準輸入和標準輸出。文件指針和文件指針可以初始化爲標準輸入 –

0

您可以使用fgets來讀取輸入字符串,並使用strtok來解析每個空間的字符串。

像這樣的東西可以工作:

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

#define NUMWORDS 400 
#define WORDLEN 40 
#define BUFFSIZE 100 

int 
main(void) { 
    char text[NUMWORDS][WORDLEN]; 
    char line[BUFFSIZE], *word; 
    size_t slen, count = 0; 
    int i; 

    printf("Enter some words: "); 
    if (fgets(line, BUFFSIZE, stdin) == NULL) { 
     printf("Error reading into buffer.\n"); 
     exit(EXIT_FAILURE); 
    } 

    slen = strlen(line); 
    if (slen > 0) { 
     if (line[slen-1] == '\n') { 
      line[slen-1] = '\0'; 
     } else { 
      printf("Exceeded buffer length of %d.\n", BUFFSIZE); 
      exit(EXIT_FAILURE); 
     } 
    } 

    if (!*line) { 
     printf("No words entered.\n"); 
     exit(EXIT_FAILURE); 
    } 

    word = strtok(line, " "); 
    while (word != NULL) { 
     if (strlen(word) >= WORDLEN) { 
      printf("\nYour word \"%s\" is longer than word size limit: %d\n", 
         word, WORDLEN); 
      exit(EXIT_FAILURE); 
     } 
     strcpy(text[count], word); 
     count++; 
     word = strtok(NULL, " \n"); 
    } 

    if (count > NUMWORDS) { 
     printf("Too many words entered.\n"); 
     exit(EXIT_FAILURE); 
    } 

    printf("\nYour array of strings:\n"); 
    for (i = 0; i < count; i++) { 
     printf("text[%d] = %s\n", i, text[i]); 
    } 

    return 0; 
} 

輸入:

Enter some words: sadsad asdasdasd asdasdasdasd 

輸出:

Your array of strings: 
text[0] = sadsad 
text[1] = asdasdasd 
text[2] = asdasdasdasd 
0

如果你只想計數輸入文本的特點?我會做這樣的事情(中頻(* S = 32)是不計空格,ASCII「空間」 =十進制32!):

int main() 
{ 
    int size = 0; 
    char input[200] = "This is an input string"; 
    size = mystrlen(input); 
    printf("size=%d", str); 
    return 0 

} 

int mystrlen(char *s) 
{ 
    int i = 0; 
    while (*s++) { 
     if (*s != 32) 
      i++; 
    } 
    return i; 
} 
output: size = 19; 
0

很簡單,因爲scanf函數讀取,直到第一個空間。 你必須使用get here。 這一個簡單的卡拉科特計數無空格:

char str [100]; 
int i=0,cpt=0; 
printf ("Enter some words: "); 
gets (str); 
While(str [i]!='\0'){ 
    if (str [i]!=' '){ 
     cpt++; 
    } 
    i++; 
} 

printf ("Size: %d",cpt); 
+0

不要再使用'gets()',它的邪惡並且不再是C的一部分,而是使用'fgets()', – alk

0

所有的scanf函數的第一個( 「%S」,&文本);你正在使用的將不會讀取任何空格。它只會讀取,直到名字中遇到第一個空格。使用scanf(「%[^/n] s」,& text);而是用空格讀取名稱。 你正在閱讀二維數組的方式是錯誤的。簡單的方法是使用for循環。

例如:

for (i=0;i<n;i++) 
{ 
    scanf ("%[^/n]s,text[i]); 
}