Computer >> 컴퓨터 >  >> 프로그램 작성 >> C 프로그래밍

C/C++의 모음과 자음 교대

<시간/>

모음과 자음이 포함된 입력 문자열이 제공됩니다. 모음과 자음이 마지막 문자열에서 교대로 자리를 차지하도록 문자열을 재배열합니다. 모음과 자음을 대체 위치에 배열할 때 입력 문자열은 다음 조건 중 하나를 충족해야 합니다. -

  • 모음과 자음의 개수는 같아야 합니다. "individual" 문자열에는 5개의 모음과 5개의 자음이 있습니다.

  • 모음의 수가 더 많으면 모음의 수와 자음의 수의 차이는 1이어야 합니다. "noe" 문자열은 모음 2개와 자음 1개로 구성됩니다.

  • 자음 수가 많으면 자음 수와 모음 수의 차이가 1이어야 합니다. "objective" 문자열에는 4개의 모음과 5개의 자음이 있습니다.

알고리즘

1. count number of vowels
2. Count number of consonants
3. if difference between number of vowels and consonants or viceversa is greater than 1 then return error
4. Split input string into two parts:
   a) First string contains only vowels
   b) Second string contains only consonants
5. If number of consonants and vowels are equal, then create final string by picking a character from each string alternatively.
6. If number of vowels are greater than consonants, then:
   a) Include additional vowel in final string to make both strings of equal length
   b) Create final string by appending a character from each string alternatively
7. If number of consonants are greater than vowels, then
   a) Include additional consonant in final string to make both strings of equal length
   b) Create final string by appending a character from each string alternatively

예시

#include <iostream>
#include <string>
using namespace std;
bool is_vowel(char ch) {
   if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch =='u') {
      return true;
   }
   return false;
}
string create_final_string(string &s1, string &s2, int start, int end) {
   string final_string;
   for (int i = 0, j = start; j < end; ++i, ++j) {
      final_string = (final_string + s1.at(i)) + s2.at(j);
   }
   return final_string;
}
string create_alternate_string(string &s) {
   int vowel_cnt, consonant_cnt;
   string vowel_str, consonant_str;
   vowel_cnt = consonant_cnt = 0;
   for (char c : s) {
      if (is_vowel(c)) {
         ++vowel_cnt;
         vowel_str += c;
      } else {
         ++consonant_cnt;
         consonant_str += c;
      }
   }
   if (abs(consonant_cnt - vowel_cnt) >= 2) {
      cerr << "String cannot be formed with alternating vowels and cosonants\n";
      exit(1);
   }
   if ((consonant_cnt - vowel_cnt) == 0) {
      return create_final_string(vowel_str, consonant_str, 0, vowel_cnt);
   } else if (vowel_cnt > consonant_cnt) {
      return vowel_str.at(0) + create_final_string(consonant_str,vowel_str, 1, vowel_cnt);
   }
   return consonant_str.at(0) + create_final_string(vowel_str,consonant_str, 1, consonant_cnt);
}
int main() {
   string s1 = "individual";
   string s2 = "noe";
   string s3 = "objective";
   cout << "Input : " << s1 << "\n";
   cout << "Output: " << create_alternate_string(s1) << "\n\n";
   cout << "Input : " << s2 << "\n";
   cout << "Output: " << create_alternate_string(s2) << "\n\n";
   cout << "Input : " << s3 << "\n";
   cout << "Output: " << create_alternate_string(s3) << "\n\n";
}

출력

위의 코드를 컴파일하고 실행하면 다음과 같은 출력이 생성됩니다 -

Input : individual
Output: inidivudal
Input : noe
Output: one
Input : objective
Output: bojecitev