0

所以我有一个由空格分隔的单词组成的字符数组。从输入中读取数组。我想打印以元音开头的单词。

我的代码:

#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到一个char中,我用它在数组的开头插入一个空格。

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

我确定问题出在主 do-while 循环的某个地方,但我无法弄清楚可能是什么......这两个功能正常工作。谢谢!

4

2 回答 2

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

这是不正确的!

于 2013-10-06T11:08:01.617 回答
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;
}
于 2013-10-06T14:32:57.860 回答