我们如何从给定的字符串中获取元音子字符串

时间:2019-01-28 19:25:26

标签: c

我想从给定的字符串中获取所有元音子字符串。 给定字符串为“ auiouxaeibaou ”,请从给定字符串中获取子字符串,例如[ auiou aei aou ]。

在这里,我尝试了类似的方法,但是没有得到确切的输出。

bool isVowel(char c) {
    return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}

void substr(char str[], int low, int high)
{
    printf("%.*s \n\n ", high-low+1, (str+low));
}

int main(int argc, const char *argv[]) {

    char str[] = "aeixae";

    int length = strlen(str);

    int start_index = 0, end_index = 0;

    for (int x=0; x<length; x++) {
        char c  = str[x];
        if (isVowel(c) == false) {
            end_index = x;
            substr(str, start_index, end_index - 1 );
            start_index = end_index + 1;
        }

    }
    return 0;
}

5 个答案:

答案 0 :(得分:2)

  1. 好的程序。请记住,使用size_t作为strlen()返回的正确类型。
  2. 好吧,如果您希望输入为auiouxaeibaou,则需要将其插入char str[] = "aeixae";
  3. 强大的substr功能!
  4. 您需要记住字符串中的最后一个子字符串-当x到达lenth时,仍然有一个子字符串

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


bool isVowel(char c) {
    return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}

void substr(char str[], int low, int high)
{
    printf("%.*s\n", high-low+1, (str+low));
}

int main(int argc, const char *argv[]) {

    // ch-ch-ch-changes   
    char str[] = "auiouxaeibaou";

    int length = strlen(str);

    int start_index = 0, end_index = 0;

    for (int x = 0; x < length; x++) {
        char c  = str[x];
        if (isVowel(c) == false) {
            end_index = x;
            substr(str, start_index, end_index - 1 );
            start_index = end_index + 1;
        }
    }

    // ch-ch-ch-changes
    end_index = length;
    substr(str, start_index, end_index - 1 );

    return 0;
}

答案 1 :(得分:2)

您的尝试已结束。我只是添加了includes,并确保字符串的最后部分也已打印。请查看for循环的终止以及if在哪里检查元音。

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

bool isVowel(char c) {
    return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}

void substr(char str[], int low, int high)
{
    printf("%.*s \n\n", high-low+1, (str+low));
}

int main(int argc, const char *argv[]) {

    char str[] = "aeixae";

    int length = strlen(str);

    int start_index = 0, end_index = 0;

    for (int x=0; x<=length; x++) {
        if (x == length || !isVowel(str[x])) {
            end_index = x;
            substr(str, start_index, end_index - 1 );
            start_index = end_index + 1;
        }

    }
    return 0;
}

这是输出:

gcc main.c && ./a.out 
aei 

ae 

答案 2 :(得分:2)

  

但没有得到确切的输出。

为什么您没有得到预期的结果:

  • 您错过了元音的最后一个序列,只是将for (int x=0; x<length; x++)替换为for (int x=0; x<=length; x++),因为空字符不是元音(不会产生非法访问)
  • 当您有多个连续的非元音时,无论如何都要调用substr,以避免您需要记住以前是否有元音

所做的修改使(我更改了输入字符串):

bool isVowel(char c) {
    return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}

void substr(char str[], int low, int high)
{
    printf("%.*s \n\n ", high-low+1, (str+low));
}

int main(int argc, const char *argv[]) {
  char str[] = "aeixaewwii";

  int length = strlen(str);

  int start_index = 0, end_index = 0;
  bool wasVowel = false;

  for (int x=0; x<=length; x++) {
    char c  = str[x];
    if (isVowel(c) == false){
      end_index = x;
      if (wasVowel)
        substr(str, start_index, end_index - 1 );
      start_index = end_index + 1;
      wasVowel = false;
    }
    else
      wasVowel = true;
  }

  return 0;
}

顺便说一句:“ y”对我来说是元音,您在 isVowel()

中错过了它

答案 3 :(得分:1)

有趣的是,这是一种无需子字符串的简单方法,只需在元音出现时就打印元音,并在第一次遇到辅音时添加换行符即可。

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

int isVowel(char c){
    if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'){
        return 1;
    }
    else{
        return -1;
    }
}

int main()
{       
   char my_str[] = "auiouxxxxxxaeibaou";

   int todo_newline = 1; // prevent consecutive newlines

   for(int i = 0; my_str[i] != '\0'; i++){
       if(isVowel(my_str[i]) == 1){
           printf("%c", my_str[i]);
           todo_newline = 1; 
       }
       else{
           if(todo_newline == 1){ // print one newline
               printf("%c", '\n');
               todo_newline = -1; // don't print consecutive newlines
           }
       }
   }
}

答案 4 :(得分:0)

替代方法:

strspn()strcspn()是最好的工具。 @Gem Taylor

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

int main(void) {
  char str[] = "aeixae";
  const char *s = str;
  while (*(s += strcspn(s, "aeiou")) != '\0') { // skip letters that are not aeiou
    size_t len = strspn(s, "aeiou");            // find length made up of aeiou
    printf("<%.*s>\n", (int) len, s);           // print sub string
    s += len;
  }
}

输出

<aei>
<ae>