2013-10-06 29 views
0

所以我有一個由空格分隔的單詞組成的字符數組。 從輸入讀取數組。我想打印以元音開頭的單詞。如何在C中打印字符串數組的特定部分?

我的代碼:

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

char *insertt (char dest[100], const char sou[10], int poz) { 

    int cnt=0; 

    char *fine = (char *) malloc(sizeof(char) * 3); 
    char *aux = (char *) malloc(sizeof(char) * 3); 

    strncpy(fine,dest,poz); 

    strcat(fine,sou); 

    do { 
     aux[cnt]=dest[poz]; 
     poz++; 
     cnt++; 
    } 
    while (poz<=strlen(dest)); 

    strcat(fine,aux); 
    free(aux); 

    return fine; 
    } 


int check_vowel(char s) { 
    switch (s) { 
     case 'a': 
     case 'e': 
     case 'i': 
     case 'o': 
     case 'u': 
     case 'A': 
     case 'E': 
     case 'I': 
     case 'O': 
     case 'U': 
      return 1; 
     default: return 0; 

    } 
} 

    int main (void) { 

     const char spc[]=" "; 
     char s[100]; 
     char *finale = (char *) malloc(sizeof(char) * 3); 
     int beg,len,aux; //beg - beginning; len - length; 
     int i = 0; 


     printf("s="); 
     gets(s); 


     finale = insertt(s,spc,0); //the array will start with a space 

     do { 
      if (finale[i]==' ' && check_vowel(finale[i+1])==1) { 
       beg=i+1; //set start point 
       do { //calculate length of the word that starts with a vowel 
        len++; 
       }   
       while(finale[len]!=' '); //stop if a space is found 
        printf("%.*s\n", len, finale + beg); //print the word 
      } 

     i++; 
     } 
     while (i<=strlen(finale)); //stop if we reached the end of the string 

     free(finale); 
     return 0; 


    } 

的check_vowel函數將返回1,如果字符是一個元音,否則返回0 的insertt功能將插入一個爲const char焦炭從一個指定的位置,我用它在數組的開頭插入一個空格。

The input: today Ollie was ill 
The output: Segmentation fault (core dumped) 
Preferred output: 
Ollie 
ill 

我敢肯定,問題是在主要do-while循環的地方,但我只是想不出什麼可能是它... 的2個功能正常工作。 謝謝!

+0

正如我所說,2個功能(insertt和檢查元音)正常工作,他們做的正是它們是什麼應該這樣做:) –

+0

將輸入讀入字符串數組不是更容易嗎? – LihO

+0

適合我:jrenner @ pc:〜$。/ test s =今天Ollie生病了 Ollie ill – talloaktrees

回答

1

那麼你的程序中有很多錯誤可能導致SIGSEGV

1)fine & aux功能insertt中的指針尚未分配足夠的內存量。變化可以說:

char *fine = malloc(sizeof(char) * 300); 

2)在所述外do while{}環,條件必須爲:

i<strlen(finale) 

3)在內部do while{},條件必須爲:

while(len<strlen(finale) && finale[len]!=' '); 

編輯

還有在代碼中logical error,這是我留給你看着辦吧:

對於字符串:

adfajf isafdadsf ifsfd 

你的輸出是:

adfajf 
isafdadsf ifsfd 
ifsfd 

這是不正確! !

+0

請不要暗示'malloc()'的返回值,即使OP的代碼包含該錯誤。 – 2013-10-06 11:16:29

+0

@ H2CO3相應編輯.. :) – nitish712

+0

謝謝,完美! – 2013-10-06 11:23:05

0

另一種方法,使用一個小的有限狀態機。字符串被修改到位breakcontinue的戰略使用避免了很多條件。 (僅存的條件它的另外一個空間的第二個及以後選擇的話)之前

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

int classify(int ch) 
{ 
switch(ch) { 
case 'a': case 'A': 
case 'e': case 'E': 
case 'i': case 'I': 
case 'o': case 'O': 
case 'u': case 'U': 
case 'y':case 'Y': 
     return 1; 
case ' ': case '\t': case '\n': 
     return 0; 
default: 
     return 2; 
     } 
} 

size_t klinkers_only(char *str) 
{ 
size_t src,dst; 
int type,state; 

for (state=0, src=dst=0; str[src] ; src++) { 
     type = classify(str[src]); 
     switch(state) { 
     case 0: /* initial */ 
       if (type == 1) { state =1; if (dst>0) str[dst++] = ' '; break; } 
       if (type == 2) { state =2; } 
       continue; 
     case 1: /* in vowel word */ 
       if (type == 0) { state=0; continue; } 
       break; 
     case 2: /* in suppressed word */ 
       if (type == 0) { state=0; } 
       continue; 
       } 
     str[dst++] = str[src]; 
     } 

str[dst] = 0; 
return dst; 
} 
int main(void) 
{ 
size_t len; 
char string[] = "abc def gh ijk lmn opq rst uvw xyz"; 

printf("String:='%s'\n", string); 

len = klinkers_only(string); 
printf("Return= %zu, String:='%s'\n", len, string); 

return 0; 
} 
相關問題