代码之家  ›  专栏  ›  技术社区  ›  Damodar

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

c
  •  2
  • Damodar  · 技术社区  · 7 年前

    我想从给定的字符串中获取所有元音子字符串。 给定的字符串是' 奥乌赛堡 ,从给定字符串中获取子字符串,如[ 奥尤 , , 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 回复  |  直到 7 年前
        1
  •  2
  •   KamilCuk    7 年前
    1. 好节目。记住使用 size_t 作为返回的正确类型 strlen() .
    2. 好吧,如果你想输入 auiouxaeibaou ,您需要将其插入到 char str[] = "aeixae";
    3. 伟大的 substr 作用
    4. 您需要记住字符串中的最后一个子字符串-when 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;
    }
    
        2
  •  2
  •   bruno    7 年前

    但是没有得到确切的输出。

    • 你错过了元音的最后一个序列,让它只是替换 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”是我的元音,你在我的字典里漏掉了 元音()

        3
  •  2
  •   Jörg Beyer    7 年前

    你的尝试很接近。我只是添加了include,并确保字符串的最后一部分也被打印出来。查看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 
    
        4
  •  1
  •   RenderedNonsense    7 年前

    有趣的是,这里有一个简单的方法,不需要子串,只要在元音出现时打印,并在第一次碰到辅音时添加新行即可。

    #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
               }
           }
       }
    }
    
        5
  •  0
  •   chux    7 年前

    备选办法:

    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>