forked from Andyy-18/CPP-BASICS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrings.cpp
104 lines (86 loc) · 1.86 KB
/
Strings.cpp
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
95
96
97
98
99
100
101
102
103
104
#include<bits/stdc++.h>
using namespace std;
// to convert uppercase to lowercase
char tolowercse(char ch){
if(ch>='a' && ch<='z')
return ch;
else{
char temp = ch - 'A' + 'a';
return temp;
}
}
bool checkpalindrome(char a[],int n){
int s=0;
int e=n-1;
while(s<=e){
if(tolowercse(a[s]) != tolowercse(a[e])){
return 0;
}
else{
s++;
e--;
}
}
return 1;
}
void reverse(char name[], int n){
int s=0;
int e=n-1;
while(s<e){
swap(name[s++],name[e--]);
}
}
int getlength(char name[]){
int count=0;
for(int i=0; name[i] != '\0'; i++){
count++;
}
return count;
}
char getMaxOccChar(string s){
int arr[26]={0};
// create an array to count of charecters
for(int i=0; i<s.length();i++){
char ch=s[i];
int num=0;
// lowercase
if(ch>='a' && ch<='z'){
num=ch-'a';
}
else{//uppercase
num=ch-'A';
}
arr[num]++;
}
// find maximum occ charecter
int maxi=-1,ans=0;
for(int i=0;i<26;i++){
if(maxi<arr[i]){
ans=i;
maxi=arr[i];
}
}
char finalans ='a'+ans;
return finalans;
}
int main()
{
/*
char name[20];
cout<<"Enter your name: "<<endl;
cin>>name;
// name[3]='\0';
int n=getlength(name);
cout<<"Your name is "<<name<<endl;
cout<<"Length : "<<n<<endl;
reverse(name,n);
cout<<"Your name is "<<name<<endl;
cout<<"Palindrome or not: "<<checkpalindrome(name,n)<<endl;
// convert uppercase to lowercase
cout<<"Charecter is "<<tolowercse('A')<<endl;
*/
string s;
cin>>s;
cout<<getMaxOccChar(s)<<endl;
return 0;
}