繁体   English   中英

C 语言:给定一个字符串,删除/删除包含 n 个元音的单词

[英]C language: Given a string, delete/remove the words that contain the n number of vowels

给定一个 integer n ,程序必须删除每个包含n个元音的单词。 该字符串是从 test.txt 文件中读取的,该文件包含以下内容: Astazi nu este maine 目前我的程序包含一个count1 function,它计算字符串中每个单词的字符数和元音数。 在输入n个元音删除所需的单词然后打印更新的字符串时,如何使用count1 function 中的数据作为参考?

我有一个想法,我不确定如何实施。 count1中,我们已经计算了每个单词的元音数量,因此,用户给定n ,我们检查这个数字是否等于count1 function 中的v ,所以我们执行int num_of_words++ ,然后我们执行一个循环,打印出需要的单词,直到num_of_words=0

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


void count1 (char* str)
{
    for (int i = 0;;)
        for (int v = 0, w = i;;)
        {
            int len;
            char c = str[i++];
            switch (c)
            {
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
                v++;
            default:
                continue;
            case ' ':
            case '\t':
            case '\n':
            case '\0':
                len = i - 1 - w;
                printf("'%.*s': %d characters, %d vowels\n", len, str+w, len, v );
                if (c)
                    break;
                else
                    return;
            }
            break;
        }
}

void count2 (char* str, int n)
{
    char line2[128];
    int ls=strlen(str);
    for (int i = 0;;)
        for (int v = 0, w = i;;)
        {
            int len;
            char c = str[i++];
            switch (c)
            {
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
                v++;
            default:
                continue;
            case ' ':
            case '\t':
            case '\n':
            case '\0':
                for(int k = 0; str[k] != '\0'; k++)
                {
                    if (k == 0 || isspace(str[k]))
                    {
                        if(v==n)
                        {
                            strcat(line2, str+1);
                        }
                    }
                }
                printf("%s ", line2);
                if (c)
                    break;
                else
                    return;
            }
            break;
        }
}

int main()
{
    FILE *fp;
    char line[128];
    int c=0, count[26]= {0}, x;
    int n;

    fp = fopen("test.txt", "r");

    fscanf(fp, "%[^\n]", line);
    fclose(fp);
    printf("%s\n\n", line);

    while (line[c] != '\0')
    {
        if (line[c] >= 'a' && line[c] <= 'z')
        {
            x = line[c] - 'a';
            count[x]++;
        }
        c++;
    }

    for (c = 0; c < 26; c++)
    {
        printf("%c occurs %d times.\n", c + 'a', count[c]);
    }
    printf("\n");
    count1(line);
    printf("\nInsert n: ");
    scanf("%d", &n);
    count2(line, n);



    return 0;
}

如果您有一个字符串str由单独的单词组成,用' ''\n''\t'将它们分开,并且您希望有一个字符串包含str中满足某些条件的所有单词,它将其编程为“就地”将有点困难,即将str更改为所需的字符串,而不使用某种“辅助数组”。

相反,我建议创建一个具有相同大小的新 char 数组(比如str2 ),并且每次找到满足条件的单词(例如:没有 1 个元音)时,复制您从strstr2找到的单词。

像这样的东西:

char str[128];
// read from file to str using fscanf
char str2[128];
for (int c = 0; str[c] != '\0'; ++c)
{
   if (c == 0 || isspace(str[c]))
   {
      if (! is_1_vowel[str+1]) // if it doesn't have exacly one vowel
      {
          // copy the word from str to str2
          strcat_word(str2, str+1); // a user-defined adapted version of strcat that will copy from src to dest only till src reaches a space character or '\0'
      }
   }
}

我在这里假设is_1_vowel将是一个 function ,它会遍历一个单词(而不是整个行或文件),如果满足条件(有 1 个元音)则返回 1,否则返回 0。

这是我的最终解决方案

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


void count1 (char* str)// count numbers of vowels for each word
{
    for (int i = 0;;)
        for (int v = 0, w = i;;)
        {
            int len;
            char c = str[i++];
            switch (c)
            {
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
                v++;
            default:
                continue;
            case ' ':
            case '\t':
            case '\n':
            case '\0':
                len = i - 1 - w;
                printf("'%.*s': %d characters, %d vowels\n", len, str+w, len, v );
                if (c)
                    break;
                else
                    return;
            }
            break;
        }
}

void print_x(char* str, int n)
{
    char* tmp;
    unsigned int cnt = 0, stat = 0;
    const char aeiou[] = "AEIOUaeiou";
    while(*str)
    {
        switch(stat)
        {
        case 0://the word did not start
            if (!isalpha(*str))
            {
                putchar(*str);
                break;
            }
            stat = 1;
            tmp = str;
            cnt = 0;
        case 1://the word started
            if (strchr(aeiou, *str))
            {
                cnt++;
                break;
            }
            if (! isalpha(*str))
            {
                if (cnt != n)
                while(tmp <= str) putchar(*(tmp++));
                else putchar(*str);
                stat = 0;
            }
        } // end switch
        ++str;
    }
    if (stat)
    {
        --str;
        if (cnt != n) while(tmp <= str) putchar(*(tmp++));
    }
}


int main()
{
    FILE *fp;
    char line[128], line2[128];
    int c=0, count[26]= {0}, x;
    int n,a;
    int i,j;

    fp = fopen("test.txt", "r");

    fscanf(fp, "%[^\n]", line);
    fclose(fp);
    printf("%s\n\n", line);

    while (line[c] != '\0')
    {
        if (line[c] >= 'a' && line[c] <= 'z')
        {
            x = line[c] - 'a';
            count[x]++;
        }
        c++;
    }

    for (c = 0; c < 26; c++)
    {
        printf("%c occurs %d times.\n", c + 'a', count[c]);
    }

    for (i = 0; i < 26; ++i)
    {
        for (j = i + 1; j < 26; ++j)
        {
            if (count[i] < count[j])
            {
                a = count[i];
                count[i] = count[j];
                count[j] = a;
            }
        }
    }
    printf("\n\n");

    for (c = 0; c < 26; c++)
    {
        printf("%c occurs %d times.\n", c + 'a', count[c]);
    }

    printf("\n");
    count1(line);
    printf("\nInsert n: ");
    scanf("%d", &n);

    if (!(fp = fopen("./test.txt", "r")))
    {
        printf("unable open file\n");
        return 1;
    }
    while (fgets(line, 128, fp))
        print_x(line, n);
    fclose(fp);

    return 0;
}

暂无
暂无

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

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