Skip to content

string problems #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 16, 2021
Merged

string problems #16

merged 1 commit into from
Oct 16, 2021

Conversation

Manish4Kumar
Copy link
Contributor

thanks

@Manish4Kumar
Copy link
Contributor Author

#4
In C++, string is an object of std::string class that represents sequence of characters. We can perform many operations on strings such as concatenation, comparison, conversion etc.
C++ String Example
Let's see the simple example of C++ string.

#include
using namespace std;
int main( ) {
string s1 = "Hello";
char ch[] = { 'C', '+', '+'};
string s2 = string(ch);
cout<<s1<<endl;
cout<<s2<<endl;
}
Output:

Hello
C++

String Data Structure

Practice Problems on String
Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’.

Declaring a string is as simple as declaring a one dimensional array. Below is the basic syntax for declaring a string in C programming language.
char str_name[size];
Problems:-
#include
#include
using namespace std;
int main ()
{
char key[] = "mango";
char buffer[50];
do {
cout<<"What is my favourite fruit? ";
cin>>buffer;
} while (strcmp (key,buffer) != 0);
cout<<"Answer is correct!!"<<endl;
return 0;
}
Output:

What is my favourite fruit? apple
What is my favourite fruit? banana
What is my favourite fruit? mango
Answer is correct!!

Count of distinct groups of strings formed after performing equivalent operation
Medium

Given an array arr[] of N strings consisting of lowercase alphabets, the task is to find the number of distinct groups of strings formed after performing the equivalent operation.
Two strings are said to be equivalent if there exists the same character in both the strings and if there exists another string that is equivalent to one of the strings in the group of equivalent string then that string is also equivalent to that group.

Examples;-
Problems:-
// C++ program for the above approach
#include<bits/stdc++.h>
using namespace std;

// Function to perform the find operation
// to find the parent of a disjoint set
int Find(vector& parent, int a)
{
return parent[a]
= (parent[a] == a ? a : Find(parent, parent[a]));
}

// Function to perform union operation
// of disjoint set union
void Union(vector& parent,
vector& rank, int a,
int b)
{

// Find the parent of node a and b
a = Find(parent, a);
b = Find(parent, b);

// Update the rank
if (rank[a] == rank[b])
rank[a]++;
if (rank[a] > rank[b])
parent[b] = a;
else
parent[a] = b;
}

// Function to find the number of distinct
// strings after performing the
// given operations
void numOfDistinctStrings(string arr[],
int N)
{
// Stores the parent elements
// of the sets
vector parent(27);

// Stores the rank of the sets
vector rank(27, 0);

for (int j = 0; j < 27; j++) {
// Update parent[i] to i
parent[j] = j;
}

// Stores the total characters
// traversed through the strings
vector total(26, false);

// Stores the current characters
// traversed through a string
vector current(26, false);

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

for (int j = 0; j < 26; j++) {

	// Update current[i] to false
	current[j] = false;
}

for (char ch : arr[i]) {

	// Update current[ch - 'a'] to true
	current[ch - 'a'] = true;
}

for (int j = 0; j < 26; j++) {

	// Check if current[j] is true
	if (current[j]) {

		// Update total[j] to true
		total[j] = true;

		// Add arr[i][0] - 'a' and
		// j elements to same set
		Union(parent, rank,
			arr[i][0] - 'a', j);
	}
}

}

// Stores the count of distinct strings
int distCount = 0;
for (int i = 0; i < 26; i++) {

// Check total[i] is true and
// parent of i is i only
if (total[i] && Find(parent, i) == i) {

	// Increment the value of
	// distCount by 1
	distCount++;
}

}

// Print the value of distCount
cout << distCount << endl;
}

// Driver Code
int main()
{
string arr[] = { "a", "ab", "b", "d" };
int N = sizeof(arr) / sizeof(arr[0]);
numOfDistinctStrings(arr, N);

return 0;
}

Output:
2

C++ String Concat Example

Let's see the simple example of string concatenation using strcat() function.

#include
#include
using namespace std;
int main()
{
char key[25], buffer[25];
cout << "Enter the key string: ";
cin.getline(key, 25);
cout << "Enter the buffer string: ";
cin.getline(buffer, 25);
strcat(key, buffer);
cout << "Key = " << key << endl;
cout << "Buffer = " << buffer<<endl;
return 0;
}
Output:

Enter the key string: Welcome to
Enter the buffer string: C++ Programming.
Key = Welcome to C++ Programming.
Buffer = C++ Programming.

C++ String Copy Example
Problems:-
Let's see the simple example of copy the string using strcpy() function.

#include
#include
using namespace std;
int main()
{
char key[25], buffer[25];
cout << "Enter the key string: ";
cin.getline(key, 25);
strcpy(buffer, key);
cout << "Key = "<< key << endl;
cout << "Buffer = "<< buffer<<endl;
return 0;
}
Output:

Enter the key string: C++ Tutorial
Key = C++ Tutorial
Buffer = C++ Tutorial

C++ String Length Example
Problems-

#include
#include
using namespace std;
int main()
{
char ary[] = "Welcome to C++ Programming";
cout << "Length of String = " << strlen(ary)<<endl;
return 0;
}

Output:

Length of String = 26

@Manish4Kumar Manish4Kumar reopened this Oct 12, 2021
@subhamengine subhamengine merged commit 8be6cd2 into subhamengine:main Oct 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants