简体   繁体   English

如何在C中打印字符串数组的特定部分?

[英]How to print specific parts of an array of strings in C?

So I have a char array formed by words separated by spaces. 所以我有一个用空格分隔的单词组成的char数组。 The array is read from the input. 从输入中读取数组。 I would like to print the words that start with a vowel. 我想打印以元音开头的单词。

My code: 我的代码:

#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;


    }

The check_vowel function will return 1 if the character is a vowel, otherwise, it returns 0. The insertt function will insert a const char into a char from a specified position, I used it to insert a space at the beginning of the array. 如果字符是元音,则check_vowel函数将返回1,否则,它将返回0。insertt函数将从指定位置将const char插入char中 ,我用它在数组的开头插入了一个空格。

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

I'm sure the problem is somewhere in the main do-while loop, but I just can't figure out what might be it... The 2 functions work correctly. 我确定问题出在主do-while循环中,但是我无法弄清楚到底是什么... 2个函数正常工作。 Thanks! 谢谢!

Well there are lot many errors in your program that may cause a SIGSEGV . 那么,您的程序中有很多错误可能会导致SIGSEGV

1) The fine & aux pointers in the function insertt haven't been allocated with sufficient amount of memory. 1)函数inserttfineaux指针尚未分配有足够的内存。 Change it to say: 更改为:

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

2) In the outer do while{} loop, the condition must be: 2)在外部do while{}循环中,条件必须为:

i<strlen(finale)

3) In the inner do while{} , the condition must be: 3)在内部do while{} ,条件必须是:

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

EDIT : 编辑

There is still a logical error in your code, which I leave it to you to figure it out: 您的代码中仍然存在logical error ,我让您自行解决:

For string: 对于字符串:

adfajf isafdadsf ifsfd

Your output was: 您的输出是:

adfajf 
isafdadsf ifsfd
ifsfd

which is incorrect!! 这是不正确的!

Alternative approach, using a small finite state machine. 替代方法,使用小型有限状态机。 The string is modified in place . 字符串被修改到位 The strategic use of break and continue avoids a lot of conditionals. breakcontinue的策略性使用避免了很多条件。 (the only remaining condition it the for the addition of a space before the second and next selected words) (唯一剩下的条件是在第二个和下一个所选单词之前添加一个空格)

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM