2017-02-21 71 views
1

我想使用C將字符串拆分成6個塊,而且我有一段艱難的時間。如果你輸入一個12個字符的長字符串,它只會打印兩個不尋常的字符將字符串拆分爲塊。

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

void stringSplit(char string[50]) 
{ 
    int counter = 0; 
    char chunk[7]; 

    for (unsigned int i = 0; i < strlen(string); i++) 
    { 
     if (string[i] == ' ') 
     { 
      continue; 
     } 

     int lastElement = strlen(chunk) - 1; 
     chunk[lastElement] = string[i]; 
     counter++; 

     if (counter == 6) 
     { 
      printf(chunk); 
      memset(chunk, '\0', sizeof chunk); 
      counter = 0; 
     } 
    } 
    if (chunk != NULL) 
    { 
     printf(chunk); 
    } 

} 

int main() 
{ 
    char string[50]; 
    printf("Input string. \n"); 
    fgets(string, 50, stdin); 
    stringSplit(string); 
    return(0); 
} 

我很感激任何幫助。

+2

這些陳述詮釋是以lastElement = strlen的(塊) - 1;並且如果(塊!= NULL) {printf(chunk); } 沒有意義。 –

+1

注意:在'for(unsigned int i = 0; i

回答

1

你的問題是在

int lastElement = strlen(chunk) - 1; 

首先,strlen計數到NULL字符的字符數。你的數組最初是未初始化的,所以這可能會導致問題。

假設你的數組充滿了NUL,並且你有,比方說,開始時有2個字符,而你正在尋找第三個字符。請記住,您的2個字符分別位於位置0和1。因此,strlen將返回2(您的字符串有2個字符),您減去一個,因此lastElement變量現在具有值1。然後,將第三個字符放在索引1處,從而覆蓋已有的第二個字符。

此外,由於您每次計算字符數量,因此效率極低。但是等一下,你已經知道你有多少個角色了(你在counter中算過他們,不是嗎?)。那麼爲什麼不使用counter來計算新字符應放置在哪裏? (注意不要犯同樣的錯誤並重寫其他內容)。

+0

Too add this to this並可能澄清:int int lastElement = strlen(chunk) - 1;'和'chunk [lastElement] = string [i];'**總是將char放在相同的位置**。 –

0

該功能是錯誤的。

本聲明

int lastElement = strlen(chunk) - 1; 

可能導致功能的未定義行爲,因爲第一陣列chunk

char chunk[7]; 

及本聲明

memset(chunk, '\0', sizeof chunk); 

後第二次開始初始化

將變量 lastElement的值等於-1。

這個if語句

if (chunk != NULL) 
{ 
    printf(chunk); 
} 

沒有意義,因爲數組chunk的第一個字符的地址總是不等於NULL

看來你的意思是以下。

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

void stringSplit(const char s[]) 
{ 
    const size_t N = 6; 
    char chunk[N + 1]; 

    size_t i = 0; 

    for (; *s; ++s) 
    { 
     if (!isspace((unsigned char)*s)) 
     { 
      chunk[i++] = *s; 

      if (i == N) 
      { 
       chunk[i] = '\0'; 
       i = 0; 
       puts(chunk); 
      } 
     } 
    } 

    if (i != 0) 
    { 
     chunk[i] = '\0'; 
     puts(chunk); 
    } 
} 

int main(void) 
{ 
    char s[] = " You and I are beginners in C "; 

    stringSplit(s); 
} 

程序輸出是

Youand 
Iarebe 
ginner 
sinC 

可以修改功能,該塊的長度被指定爲函數參數的方式。

例如

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

void stringSplit(const char s[], size_t n) 
{ 
    if (n) 
    { 
     char chunk[n + 1]; 

     size_t i = 0; 

     for (; *s; ++s) 
     { 
      if (!isspace((unsigned char)*s)) 
      { 
       chunk[i++] = *s; 

       if (i == n) 
       { 
        chunk[i] = '\0'; 
        i = 0; 
        puts(chunk); 
       } 
      } 
     } 

     if (i != 0) 
     { 
      chunk[i] = '\0'; 
      puts(chunk); 
     } 
    }  
} 

int main(void) 
{ 
    char s[] = " You and I are beginners in C "; 

    for (size_t i = 3; i < 10; i++) 
    { 
     stringSplit(s, i); 
     puts(""); 
    }  
} 

程序輸出將是

You 
and 
Iar 
ebe 
gin 
ner 
sin 
C 

Youa 
ndIa 
rebe 
ginn 
ersi 
nC 

Youan 
dIare 
begin 
nersi 
nC 

Youand 
Iarebe 
ginner 
sinC 

YouandI 
arebegi 
nnersin 
C 

YouandIa 
rebeginn 
ersinC 

YouandIar 
ebeginner 
sinC