-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ3.cpp
More file actions
94 lines (82 loc) · 2.27 KB
/
Q3.cpp
File metadata and controls
94 lines (82 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// 3. String Manipulation
// a. Accepts a string from the user.
// b. Checks whether the string is a palindrome (ignoring spaces and case sensitivity).
// c. Counts and displays the frequency of each character in the string (case insensitive).
// d. Replace all vowels in the string with a specific character (e.g., *).
#include <iostream>
#include <string>
using namespace std;
void checkPalindrome(string str){
string str2;
int flag = 0;
for(int i=0;i<str.length();i++){
if(str[i] != ' '){
str2+=tolower(str[i]);
}
}
for(int j=0;j<str2.length();j++){
if(str2[j] != str2[str2.length()-j-1]){
flag = 1;
break;
}
}
if(flag == 0){
cout<<"The string is a palindrome."<<endl;
}else{
cout<<"The string is not a palindrome."<<endl;
}
}
void counter(string str){
string str2;
for(int i=0;i<str.length();i++){
if(str[i] != ' '){
str2+=tolower(str[i]);
}
}
int arr[str2.length()];
for(int j=0;j<str2.length();j++){
int count = 0;
for(int k=0;k<str2.length();k++){
if(str2[j] == str2[k]){
count++;
}
}
// Store count in array
arr[j] = count;
// Check if this character hasn't been printed before
bool printed = false;
for(int p=0; p<j; p++){
if(str2[p] == str2[j]){
printed = true;
break;
}
}
// Print only if character hasn't been printed before
if(!printed){
cout << str2[j] << ": " << count << endl;
}
}
}
void replacement(string str){
string str3;
for(int j=0;j<str.length();j++){
if(str[j] == 'a' || str[j] == 'e' || str[j] == 'i' || str[j] == 'o' || str[j] == 'u' || str[j] == 'A' || str[j] == 'E' || str[j] == 'I' || str[j] == 'O' || str[j] == 'U'){
str3+='*';
}else{
str3+=str[j];
}
}
cout<<"String after replacing vowels with * : "<<str3<<endl;
}
int main(){
string str;
int flag=0;
cout<<"Enter a string : ";
getline(cin,str);
checkPalindrome(str);
cout<<endl;
counter(str);
cout<<endl;
replacement(str);
return 0;
}