KEMBAR78
PROGRAMS | PDF | Namespace | Computer Programming
0% found this document useful (0 votes)
17 views8 pages

PROGRAMS

The document contains various C++ code snippets demonstrating string manipulation techniques, including reversing a string, checking for palindromes, counting vowels and other character types, removing duplicate characters, counting character frequency, counting words, and reversing words in a string. Each technique is presented with both brute force and optimal implementations. The code is structured with appropriate comments and includes necessary libraries.

Uploaded by

Venkata Naresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views8 pages

PROGRAMS

The document contains various C++ code snippets demonstrating string manipulation techniques, including reversing a string, checking for palindromes, counting vowels and other character types, removing duplicate characters, counting character frequency, counting words, and reversing words in a string. Each technique is presented with both brute force and optimal implementations. The code is structured with appropriate comments and includes necessary libraries.

Uploaded by

Venkata Naresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

1.

Reverse a String
//Brute Force
#include<bits/stdc++.h>
using namespace std;
int main(){
string name;
cin>>name;
string s="";
for(int i=name.size()-1;i>=0;i--){
s=s+name[i];
}
cout<<s;
}

//Optimal
#include<bits/stdc++.h>
using namespace std;
int main(){
string name;
cin>>name;
int l=0;
int r=name.size()-1;
while(l<=r){
// swap(name[l],name[r]);
char ch=name[l];
name[l]=name[r];
name[r]=ch;
l++;
r--;
}
cout<<name;
}
2.Check string is palindrome
//brute
#include<bits/stdc++.h>
using namespace std;
bool ispalindrome(string name,int n){
string s="";
for(int i=n-1;i>=0;i--){
s=s+name[i];
}
return s==name;
}
int main(){
string name;
cin>>name;
int n=name.size();
if(ispalindrome(name,n)){
cout<<"true";
}
else{
cout<<"false";
}

}
// Optimal
#include<bits/stdc++.h>
using namespace std;
bool ispalindrome(string name,int n){
int l=0;
int r=n-1;
while(l<=r){
if(name[l]!=name[r]){
return false;
}
l++;
r--;
}
return true;
}
int main(){
string name;
cin>>name;
int n=name.size();
if(ispalindrome(name,n)){
cout<<"true";
}
else{
cout<<"false";
}

}
3.Count Vowels,Alphabets,Special chars,Numbers
//brute
#include<bits/stdc++.h>
using namespace std;
int main(){
string name;
getline(cin,name);
int n=name.size();
int vc=0;
int cn=0;
int sp=0;
int nm=0;
for(int i=0;i<n;i++){
if(name[i]=='A' || name[i]=='a'||name[i]=='e' ||name[i]=='E'||
name[i]=='I' ||name[i]=='i' || name[i]=='O'||name[i]=='o' ||
name[i]=='u'||name[i]=='U'){
vc++;
}
else if(name[i]>=48 && name[i]<=57){
nm++;
}
else if(name[i]>=32 && name[i]<=47){
sp++;
}
else{
cn++;
}
}
cout<<vc<<" ";
cout<<cn<<" ";
cout<<sp<<" ";
cout<<nm<<" ";
}
#Optimal
#include<bits/stdc++.h>
using namespace std;
bool isVowel(char ch){
ch=tolower(ch);
if(ch=='a'||ch=='e' ||ch=='i' || ch=='o' || ch=='u'){
return true;
}
return false;
}
int main(){
string name;
getline(cin,name);
int n=name.size();
int vc=0;
int cn=0;
int sp=0;
int nm=0;
for(char ch:name){
if(isalpha(ch)){
if(isVowel(ch)){
vc++;
}
else{
cn++;
}
}
else if(isdigit(ch)){
nm++;
}
else{
sp++;
}
}
cout<<vc<<" ";
cout<<cn<<" ";
cout<<sp<<" ";
cout<<nm<<" ";
}
// Remove duplicate characters from a string//
#include<bits/stdc++.h>
using namespace std;
int main(){
string n;
cin>>n;
unordered_set<char> st;
string res="";
for(int i=0;i<n.size();i++){
if(st.find(n[i])==st.end()){
res=res+n[i];
st.insert(n[i]);
}
}
cout<<res;
}

// Count frequency
// Frequency of characters in a string

#include<bits/stdc++.h>
using namespace std;
int main(){
string n;
cin>>n;
int cnt=0;
char ch;
for(int i=0;i<n.size();i++){
cnt=1;
bool vis=false;
for(int k=0;k<i;k++){
if(n[k]==n[i]){
vis=true;
break;
}
}
if(vis) continue;
for(int j=i+1;j<n.size();j++){
if(n[i]==n[j]){
cnt++;

}
}
cout<<n[i]<<cnt<<" ";
}

// Frequency of characters in a string

#include<bits/stdc++.h>
using namespace std;
int main(){
string n;
cin>>n;
int cnt=0;
char ch;
map<char,int> mpp;
for(int i=0;i<n.size();i++){
mpp[n[i]]++;
}

for(auto it:mpp){
cout<<it.first<<it.second<<" ";
}

}
#Count words in a string
// Number of words in a string

#include<bits/stdc++.h>
using namespace std;
int main(){
string n;
getline(cin,n);
stringstream ss(n);
string word;
int cnt=0;
while(ss>>word){
cnt++;
}
cout<<cnt;
}

Reverse words in a string


class Solution {
public:
string reverseWords(string s) {
stringstream ss(s);
string word, result = "";

while(ss >> word){


result = word + " " + result;
}

// Remove trailing space


if (!result.empty() && result.back() == ' ') {
result.pop_back();
}

return result;
}
};

You might also like