0% found this document useful (0 votes)
5 views6 pages

Java To CPP Programs Strings

The document contains C++ programs converted from Java that demonstrate various string manipulation techniques. It includes functions to check for anagrams, count word frequencies, insert one string into another at a specified index, and check if a string is a palindrome. Each program is accompanied by a main function that interacts with the user to provide input and display results.

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)
5 views6 pages

Java To CPP Programs Strings

The document contains C++ programs converted from Java that demonstrate various string manipulation techniques. It includes functions to check for anagrams, count word frequencies, insert one string into another at a specified index, and check if a string is a palindrome. Each program is accompanied by a main function that interacts with the user to provide input and display results.

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/ 6

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;
}

Page 3

You might also like