हमें कैरेक्टर्स की एक स्ट्रिंग दी गई है और कार्य है उसमें उन जोड़ों की संख्या गिनना जिनमें दोनों तत्व स्वर (vowels) हैं। अंग्रेजी वर्णमाला में पांच स्वर होते हैं: a, i, e, o, u और बाकी व्यंजन (consonants) होते हैं।
इनपुट - string str = "tutorials point"
आउटपुट - दिए गए स्ट्रिंग में स्वरों के जोड़ों की संख्या: 2
व्याख्या - दी गई स्ट्रिंग से हम जोड़े बना सकते हैं: (t,u), (u,t), (t,o), (o,r), (r,i), (i,a), (a,l), (l,s), (s,p), (p,o), (o,i), (i,n), (n,t)। जिन जोड़ों में दोनों तत्व स्वर हैं वे हैं (i,a) और (o,i), इसलिए स्वरों के जोड़ों की संख्या 2 है।
उपयोग किया गया दृष्टिकोण
- स्ट्रिंग को इनपुट करें और उसकी लंबाई ज्ञात करें।
- एक अस्थायी वेरिएबल
countलें जो स्वर जोड़ों की गिनती स्टोर करेगा। - लूप चलाएं i = 0 से length-1 तक।
- अंदर जांचें: यदि
str[i]स्वर है औरstr[i+1]भी स्वर है, तोcount++करें। - काउंट वापस करें और प्रिंट करें।
उदाहरण (C++ कोड)
#include <bits/stdc++.h>
using namespace std;
int count_pairs(string str, int length) {
int count = 0;
for (int i = 0; i < length - 1; i++) {
if (str[i]=='a' || str[i]=='i' || str[i]=='e' || str[i]=='o' || str[i]=='u') {
if (str[i+1]=='a' || str[i+1]=='i' || str[i+1]=='e' || str[i+1]=='o' || str[i+1]=='u') {
count++;
}
}
}
return count;
}
int main() {
string str = "tutorials point";
int length = str.length();
cout << "Count the pairs of vowels in the given string are: " << count_pairs(str, length);
return 0;
} आउटपुट
Count the pairs of vowels in the given string are: 2