0% found this document useful (0 votes)
4 views8 pages

Strings Basics

The document contains various code snippets in C++, Java, and other programming languages, demonstrating string manipulation techniques such as reversing strings, removing vowels, converting to lowercase, and counting characters. It also includes examples of checking for palindromes, removing special characters, and counting words in a string. The snippets illustrate common programming tasks and algorithms related to string processing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

Strings Basics

The document contains various code snippets in C++, Java, and other programming languages, demonstrating string manipulation techniques such as reversing strings, removing vowels, converting to lowercase, and counting characters. It also includes examples of checking for palindromes, removing special characters, and counting words in a string. The snippets illustrate common programming tasks and algorithms related to string processing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

}

int main() {

string s="abcdcb";

string rev=s;

reverse(s.begin(),s.end()); int main() {

if(rev==s){ string s="my name is Karthik";

cout<<"true"; for (int i = 0; i < s.length(); i++) // converting


given string to lowercase
}else{
{
cout<<"false";
s[i] = towlower(s[i]);
}
}

return 0;
int cv=0,cc=0,cs=0;
}
for(int i=0;i<s.length();i++){

if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||
class Solution {
s[i]=='u'){
public boolean isPalindrome(int x) {
cv++;
int r=0;
}else if(s[i]==' '){
int e=x;
cs++;
while(e>0){
}else{
int d=e%10;
cc++;
r=(r*10)+d;
}
e=e/10;

}
}
return x==r;
cout<<cv<<" "<<cs<< " "<<cc;
}
return 0; }

} JAVA

class Solution {

public String removeVowels(String s) {

StringBuilder ans = new StringBuilder();

for (int i = 0; i < s.length(); ++i) {

char c = s.charAt(i);

if (!(c == 'a' || c == 'e' || c == 'i' || c == 'o'


|| c == 'u')) {

ans.append(c);

}
int main() {
}
char c='e';
return ans.toString();
cout<<"ASCII VALUE OF CHAR"<< int(c);
}

}
return 0;

}
Cpp

class Solution {

public:

string removeVowels(string s) {

string ans;

for (char& c : s) {

int main() { if (!(c == 'a' || c == 'e' || c == 'i' || c == 'o'


|| c == 'u')) {
string s="my name is karthik";
ans += c;
for(int i=0;i<s.length();i++){
}
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||
s[i]=='u'){ }

s.erase(i,1); return ans;

} }

} };

cout<<s;

return 0;
}

return ans;

Cpp

// User function template for C++

int main() {

string s="my name is karthik"; class Solution {

for(int i=0;i<s.length();i++){ public:

if(s[i]==' '){ string removeSpecialCharacter(string s) {

s.erase(i,1); // code here

} string ans="";

} for(int i=0;i<s.length();i++){

cout<<s; if((s[i]>='a' && s[i]<='z')||


(s[i]>='A'&&s[i]<='Z')){
return 0;
ans+=s[i];
}
}

if(ans.empty()){

return "-1";

return ans;

};
string solve(string str, int n) {

string ans; Java


for (int i = 0; i < n; i++) { public static String removeSpecialCharacter(String
int ascii = (int) str[i]; // ascii value s) {

StringBuilder ans = new StringBuilder();

if ((ascii >= 65 && ascii <= 90) || (ascii >= 97 &&


ascii <= 122)) // if alphabets // Walk through each char and keep only
ans.push_back(str[i]); letters (a-z, A-Z)
for (int i = 0; i < s.length(); i++) {

char c = s.charAt(i);

if (Character.isLetter(c)) { // same as
Reverse a string
isalpha() in C++ int main() {
ans.append(c); string s="hello";
} for(int i=s.length()-1;i>=0;i--){
} cout<<s[i];

}
// If we never appended anything, return "-1"

return ans.length() == 0 ? "-1" : ans.toString();

int main() {

string s=“take u forward is awesome";

int n=s.length();

s[0]=toupper(s[0]);

s[n-1]=toupper(s[n-1]);

for(int i=1;i<n-1;i++){
int main() {
if(s[i]==' '){
string s="a+((b-c)+d)";
s[i-1]=toupper(s[i-1]);
for(int i=s.length();i>=0;i--){
s[i+1]=toupper(s[i+1]);
if(s[i]=='(' || s[i]==')'){

s.erase(i,1);
}
}
}
}
cout<<s;
cout<<s;

return 0;
return 0;
}
}
int main() {

string s=“google";

int n=s.length();

int freq[200];

for(int i=0;i<n;i++){

if(s[i]==' '){
int main() {
continue;
string s=“aaaabbcaff";
}else{
int n=s.length();
freq[s[i]-'a']++;
map<char,int>mp;
}
for(int i=0;i<n;i++){
}
mp[s[i]]++;
for(int i=0;i<n;i++){
}
if(freq[s[i]-'a']==1 ){
for(auto it:mp){
cout<<s[i]<<" ";
cout<<it.first<<it.second<<" ";
}
}
}
return 0;
return 0;
}
} mp[s[i]]++;

int maxi=INT_MIN;

char ans;

for(auto it:mp){

if(it.second>maxi){

maxi=it.second;
int main() {
ans=it.first;
string s1="tak";
}
string s2="cat";
}
sort(s1.begin(),s1.end());
cout<<ans;
sort(s2.begin(),s2.end());
return 0;
if(s1==s2){
}
cout<<"they are anagrams";

}else{

cout<<"no";

return 0;

} int main() {

string s="bcabc";

int n=s.length();

set<char>st;

string res=" ";

for(int i=0;i<n;i++){

if(st.find(s[i])==st.end()){

res+=s[i];

st.insert(s[i]);
int main() { }}
string s="apple"; cout<<res;
int n=s.length(); return 0;
map<char,int>mp;

for(int i=0;i<n;i++){
} }

int main() {
int main() {
string s1="abcdef";
string s="sinstriiintng";
string s2="cefz";
int n=s.length();
unordered_map<char,int>mp;
map<char,int>mp;
for(int i=0;i<s2.length();i++){
string res=" ";
mp[s2[i]]++;
for(int i=0;i<n;i++){
}
mp[s[i]]++;
for(int i=0;i<s1.length();i++){
}
if(!mp[s1sss[i]]){
for(auto it:mp){
cout<<s1[i];
if(it.second>1){
}
cout<<it.first<<" -"<<it.second<<endl;
}
}
return 0;
}
}

return 0;

int main() {

string s="hi my name is karthik";


int count=1;

int n=s.length();

for(int i=0;i<n;i++){

if(s[i]==' '){

count++;

cout<<count;

return 0;

//freq of an element in java

import java.util.*;

class Main {

public static void main(String[] args) {

String s="aaaabbc";

Map<Character, Integer>mp=new
HashMap<>();

for(char c : s.toCharArray()){

mp.put(c,mp.getOrDefault(c,0)+1);

System.out.println(mp);

You might also like