簡體   English   中英

字符串數組中一個單詞中元音的最大數量

[英]Maximum number of vowels in a word in string array

我編寫了一個程序來在字符串數組中輸入 2 個字符串。 然后打印列表中存儲的最大元音。 我哪里出錯了,有沒有更優雅的方法。

#include<iostream.h>
#include<string.h>

int main()
int i,j,c=0,k=0,maxo=0,len1,maxo1=0,len3;
char vow[] = "AEIOUaeiou";
char list[100][100],vow[]={"AEIOUaeiou"};
for(i=0;i<2;i++)  {
    cout<<"Enter word:  ";
    gets(list[i]);

    for(i=0;i<2;i++) {
        len1=strlen(list[i]);
        for(k=0;k<len1;k++) {
            for(j=0;list[j][k]!='\0';j++)
                if(list[j][k]==vow[j])
                    c++;
        }

        if(c>maxo)
            maxo=c;
        c=0;
    }

    cout<<"Maximum Vowel count:"<<maxo<<endl; 
}
  fflush(stdin);
  getchar();
  return 0;
  }

我試圖合並此代碼的更大程序。代碼中包含必要的注釋。我真的無法理解我在最后一部分中出錯的地方。 我應該首先包含最后一點代碼以便程序運行嗎?

#include<iostream.h>
#include<string.h>
int main()
{
 int i,n,len=0,sum=0,j,max,min,c=0,c2=0,k=0,maxo=0,len1,maxi=0,c1=0,len2;
float avg;
char list[100][100] = { 0 };
char vow[] = "AEIOUaeiou";
for(i=0;i<2;i++) 
{
  cout<<"Enter word:  ";
  gets(list[i]);

  len=strlen(list[i]);                                
  sum=sum+len;
  cout<<"Length of word:  "<<len<<endl;
  if(list[i][len-1]=='s')
  {cout<<"The Word "<<list[i]<<" ends with s"<<endl;
   c2++;
  }

  }
  //Word input by user.Prints word along with length.       
  min=strlen(list[0]);
  max=strlen(list[0]);
  //Initialising max and min.
  for(i=0;i<2;i++)
  {
    if(strlen(list[i])<min)
    {min=strlen(list[i]);}
    if(strlen(list[i])>max)
    {max=strlen(list[i]);}

  }
  for(i=0;i<2;i++)
  {
  if(max==strlen(list[i]))
  cout<<"The max value of the lengths stored:"<<list[i]<<endl<<"Word count:"<<max<<endl;               
  if(min==strlen(list[i]))
  cout<<"The min value of the lengths stored:"<<list[i]<<endl<<"Word count:"<<min<<endl;
  }
  //Max and Min value of string lengths are printed.
  avg=sum/2; 
  cout<<"Avg length:"<<avg<<endl;
  //Average value printed.
  cout<<"The number of words with s:"<<c2<<endl;
  //Word ending with s.


  {for (i = 0; i <2; i++) 

    len1 = strlen(list[i]);
    for (k = 0; k < len1; k++) 
    {
        for (j = 0; j < strlen(vow); j++)
            //if (list[j][k] == vow[j])
            if (list[i][k] == vow[j])
                c++;
    }
    cout << "Number of vowels in line " << i << ": " << c << '\n';
    if (c>maxo) maxo = c;
    c = 0;
    cout << "Maximum Vowel count so far:" << maxo << "\n\n";

     cout << "Maximum Vowel count:" << maxo << endl;
       }
for(i = 0 ;i < 2 ;i++)
{ len3 = strlen(list[i]);
letter = list[i][0];
{for(j=0;j<len3;j++)
if(list[i][j]==letter)
 counter++;

 }
 cout << "Number of identical letters as  first letter in line " << i << ": 
 " << counter << '\n';
 if (c>maxo1) maxo1 = counter;
 counter = 0;
 cout << "Maximum letter count so far:" << maxo1 << "\n\n";

 cout << "Maximum letter count:" << maxo1 << endl;
 }

PS:

我再次編輯了我的代碼以顯示作為列表中單詞的起始字母出現次數最多的字母表,以及它出現的次數。

這不會為我編譯,原因有兩個:

1) 獲取()

C 標准 (2011) 的最新修訂版已明確地從其規范中刪除了此功能。 該函數在 C++ 中已被棄用(從 2011 標准開始,遵循 C99+TC3)。

所以我不能使用gets() 函數。

2)你不能申報

char list[100][100], char vow[] = {"AEIOUaeiou"};

兩者都帶有逗號分隔符。

您將第一行字符串的輸入讀入數組 i = 0 的第一行; 然后你立即循環遍歷 i,這是沒有意義的。 以下不是一個好的解決方案,因為在 C++ 中你應該使用 std::vectors 和 std::string,並且通常不混合 C 和 C++,但我試圖讓它與你的版本盡可能接近,使用我的心靈感應能力讀懂你想做什么。

#include <iostream>
#include <cstring>

using namespace std;

const int numLinesToGet = 10;
const int maxCharsPerLine = 100;

int main()
{
    int i, j, c = 0, k = 0, maxo = 0, len1;

    //char list[100][100], char vow[] = {"AEIOUaeiou"};
    char list[100][100] = { 0 };
    char vow[] = "AEIOUaeiou";

    //for (i = 0; i < 2; i++)
    for (i = 0; i < numLinesToGet; i++) 
    {
        cout << "Enter word:  ";
        std::cin.getline(list[i], maxCharsPerLine);
        //gets(list[i]);

        //for (i = 0; i < 2; i++) Get rid of this second loop entirely
        len1 = strlen(list[i]);
        for (k = 0; k < len1; k++) 
        {
            //for (j = 0; list[j][k] != '\0'; j++)
            for (j = 0; j < sizeof(vow); j++)
                //if (list[j][k] == vow[j])
                if (list[i][k] == vow[j])
                    c++;
        }
        cout << "Number of vowels in line " << i << ": " << c << '\n';
        if (c>maxo) maxo = c;
        c = 0;
        cout << "Maximum Vowel count so far:" << maxo << "\n\n";
    }
    cout << "Maximum Vowel count:" << maxo << endl;

    fflush(stdin);
    getchar();
    return 0;
}

在線示例在這里

#include<stdio.h>

int main ()
{
    char a[] = "i love to code in education";
    int i, count = 0, vow = 0, mvow = 0;
    for (i = 0; a[i] != '\0'; i++)
      {
        if (a[i] == 'a' || a[i] == 'e' || a[i] == 'i' || a[i] == 'o'
            || a[i] == 'u')
          {
            vow++;
          }
        if (a[i]==' ')
        {
          count++;
          mvow = vow;
          vow = 0;
        }
     }

printf ("Total words: %d\n", count+1);
if(vow>mvow) printf ("Max Vowels in a word: %d", vow);
else printf("Max Vowels in a word: %d", mvow);
  return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM