0% found this document useful (0 votes)
2 views5 pages

Java To CPP Programs Strings FIXED

The document contains C++ programs that demonstrate various string manipulation techniques, including checking for anagrams, counting word frequencies, inserting one string into another, checking for palindromes, counting even and odd length words, retrieving characters by index, converting vowels to uppercase, counting special characters, and reversing sentences. Each program includes a main function that prompts the user for input and displays the result. The programs utilize standard libraries such as iostream, string, and algorithm.

Uploaded by

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

Java To CPP Programs Strings FIXED

The document contains C++ programs that demonstrate various string manipulation techniques, including checking for anagrams, counting word frequencies, inserting one string into another, checking for palindromes, counting even and odd length words, retrieving characters by index, converting vowels to uppercase, counting special characters, and reversing sentences. Each program includes a main function that prompts the user for input and displays the result. The programs utilize standard libraries such as iostream, string, and algorithm.

Uploaded by

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

Java to C++ Converted Programs - Strings

1. Check if Two Strings are Anagrams


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

bool isAnagram(string st1, string st2) {


int count1[26] = {0}, count2[26] = {0};
for (char ch : st1) {
if (isalpha(ch)) count1[tolower(ch) - 'a']++;
}
for (char ch : st2) {
if (isalpha(ch)) count2[tolower(ch) - 'a']++;
}
for (int i = 0; i < 26; i++) {
if (count1[i] != count2[i]) return false;
}
return true;
}

int main() {
string st1, st2;
cout << "Enter the first string: ";
getline(cin, st1);
cout << "Enter the second string: ";
getline(cin, st2);
if (isAnagram(st1, st2))
cout << "Given words are anagram" << endl;
else
cout << "Given words are not anagram" << endl;
return 0;
}

2. Frequency of Words in a String


#include <iostream>
#include <sstream>
#include <map>
using namespace std;

int main() {
string str;
cout << "Enter the string: ";
getline(cin, str);
transform(str.begin(), str.end(), str.begin(), ::tolower);

map<string, int> wordCount;


stringstream ss(str);
string word;

Page 1
Java to C++ Converted Programs - Strings

while (ss >> word) {


wordCount[word]++;
}

for (auto &pair : wordCount) {


cout << pair.first << " -> " << pair.second << endl;
}

return 0;
}

3. Insert One String into Another at Given Index


#include <iostream>
using namespace std;

string addString(string st1, string st2, int index) {


if (index < 0 || index > st1.length())
return "Invalid Index";
return st1.substr(0, index) + st2 + st1.substr(index);
}

int main() {
string st1, st2;
int index;
cout << "Enter the First String: ";
cin >> st1;
cout << "Enter the Index: ";
cin >> index;
cout << "Enter the Second String: ";
cin >> st2;
cout << addString(st1, st2, index) << endl;
return 0;
}

4. Check if a String is Palindrome


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

bool isPalindrome(string st) {


transform(st.begin(), st.end(), st.begin(), ::tolower);
int f = 0, l = st.length() - 1;
while (f < l) {
if (st[f++] != st[l--])
return false;
}
return true;
}

Page 2
Java to C++ Converted Programs - Strings

int main() {
string st;
cout << "Enter the string: ";
cin >> st;
if (isPalindrome(st))
cout << "Is Palindrome" << endl;
else
cout << "Not a Palindrome" << endl;
return 0;
}

5. Count Even and Odd Length Words in a String


#include <iostream>
#include <sstream>
using namespace std;

int* getEvenOddCount(string str) {


static int wordCount[2] = {0, 0};
stringstream ss(str);
string word;

while (ss >> word) {


if (word.length() % 2 == 0)
wordCount[0]++;
else
wordCount[1]++;
}

return wordCount;
}

int main() {
string str;
cout << "Enter the string: ";
getline(cin, str);
int* count = getEvenOddCount(str);
cout << "Number of Even charactered words: " << count[0] << endl;
cout << "Number of Odd charactered words: " << count[1] << endl;
return 0;
}

6. Get Character at Index Without Using charAt()


#include <iostream>
using namespace std;

char getCharacter(string st, int index) {


if (index < 0 || index >= st.length())
return '!';

Page 3
Java to C++ Converted Programs - Strings

return st[index];
}

int main() {
string st;
int index;
cout << "Enter the String: ";
getline(cin, st);
cout << "Enter the index: ";
cin >> index;

char ch = getCharacter(st, index);


if (ch == '!')
cout << "Invalid Index Entered" << endl;
else
cout << ch << endl;

return 0;
}

7. Convert Vowels to Uppercase and Consonants to Lowercase


#include <iostream>
#include <string>
#include <cctype>
using namespace std;

bool isVowel(char ch) {


ch = tolower(ch);
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
}

void convertString(string st) {


for (char &ch : st) {
if (isalpha(ch)) {
if (isVowel(ch))
ch = toupper(ch);
else
ch = tolower(ch);
}
}
cout << st << endl;
}

int main() {
string st;
cout << "Enter the string: ";
getline(cin, st);
convertString(st);
return 0;
}

Page 4
Java to C++ Converted Programs - Strings

8. Count Special Characters in a String


#include <iostream>
#include <string>
using namespace std;

int getSplCount(string st) {


int splcount = 0;
for (char ch : st) {
if (!isalnum(ch))
splcount++;
}
return splcount;
}

int main() {
string st;
cout << "Enter the string: ";
getline(cin, st);
int count = getSplCount(st);
cout << "The number of special characters: " << count << endl;
return 0;
}

9. Reverse the Entire Sentence Character-wise


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

string reverseWords(string str) {


reverse(str.begin(), str.end());
return str;
}

int main() {
string str;
cout << "Enter the string: ";
getline(cin, str);
string rs = reverseWords(str);
cout << "Reversed String: " << rs << endl;
return 0;
}

Page 5

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy