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

Strings: Void Int For Int

The document contains 10 problems related to strings: 1. Run length encoding of a string. 2. Counting vowels in a string iteratively and recursively. 3. Reversing a string by swapping characters from both ends. 4. Converting a string to uppercase or lowercase using transform() and tolower()/toupper(). 5. Capitalizing the first letter of each word in a string. It then continues to discuss problems involving string permutations, anagrams, removing vowels, minimum parentheses needed for validity, and generating all substrings of a given string. Solutions provided use techniques like backtracking, recursion, regular expressions, and nested loops.

Uploaded by

Rohan kolhatkar
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)
118 views6 pages

Strings: Void Int For Int

The document contains 10 problems related to strings: 1. Run length encoding of a string. 2. Counting vowels in a string iteratively and recursively. 3. Reversing a string by swapping characters from both ends. 4. Converting a string to uppercase or lowercase using transform() and tolower()/toupper(). 5. Capitalizing the first letter of each word in a string. It then continues to discuss problems involving string permutations, anagrams, removing vowels, minimum parentheses needed for validity, and generating all substrings of a given string. Solutions provided use techniques like backtracking, recursion, regular expressions, and nested loops.

Uploaded by

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

STRINGS

1. RUN LENGTH ENCODE PROBLEM


Given an input string, write a function that returns the Run Length Encoded string
for the input string.
For example, if the input string is “wwwwaaadexxxxxx”, then the function should
return “w4a3d1e1x6”

1 void printRLE(string str)


2{
3 int n = str.length();
4 for (int i = 0; i < n; i++) {
5
6 // Count occurrences of current character
7 int count = 1;
8 while (i < n - 1 && str[i] == str[i + 1]) {
9 count++;
10 i++;
11 }
12
13 // Print character and its count
14 cout << str[i] << count;
15 }
16 }

2. NO OF VOWEL IN STRING
Given a string, count the total number of vowels (a, e, i, o, u) in it. There are two
methods to count total number of vowels in a string.
Examples:
Input : abc de
Output : 2
1. Iterative

1 bool isVowel(char ch)


2{
3 ch = toupper(ch);
4 return (ch=='A' || ch=='E' || ch=='I' ||
5 ch=='O' || ch=='U');
6}
7
8 // Returns count of vowels in str
9 int countVowels(string str)
10 {
11 int count = 0;
12 for (int i=0; i<str.length(); i++)
13 if (isVowel(str[i])) // Check for vowel
14 ++count;
15 return count;
16 }
2. Recursive
1 bool isVowel(char ch)
2{
3 ch = toupper(ch);
4 return (ch=='A' || ch=='E' || ch=='I' ||
5 ch=='O' || ch=='U');
6 }
7
8 // to count total number of vowel from 0 to n
9 int countVovels(string str, int n)
10 {
11 if (n == 1)
12 return isVowel(str[n-1]);
13
14 return countVovels(str, n-1) + isVowel(str[n-1]);
15 }

3. REVERSE THE STRING


1 void reverseStr(string& str)
2{
3 int n = str.length();
4
5 // Swap character starting from two
6 // corners
7 for (int i = 0; i < n / 2; i++)
8 swap(str[i], str[n - i - 1]);
9}

4. UPPERCASE TO LOWERCASE/VICE VERSA


1 // C++ program to convert whole string to
2 // uppercase or lowercase using STL.
3 #include<bits/stdc++.h>
4 using namespace std;
5
6 int main()
7{
8 // su is the string which is converted to uppercase
9 string su = "Jatin Goyal";
10
11 // using transform() function and ::toupper in STL
12 transform(su.begin(), su.end(), su.begin(), ::toupper);
13 cout << su << endl;
14
15 // sl is the string which is converted to lowercase
16 string sl = "Jatin Goyal";
17
18 // using transform() function and ::tolower in STL
19 transform(sl.begin(), sl.end(), sl.begin(), ::tolower);
20 cout << sl << endl;
21
22 return 0;
23 }

5. CAPITALISE EACH WORD STARTING LETTER


EXAMPLE: -INPUT: I am arjun
Output:- I Am Arjun
1 string Capitalize_first_letter(string text) {
2
3 for (int x = 0; x < text.length(); x++)
4 {
5 if (x == 0)
6 {
7 text[x] = toupper(text[x]);
8 }
9 else if (text[x - 1] == ' ')
10 {
11 text[x] = toupper(text[x]);
12 }
13 }
14
15 return text;
16 }

6. PRINT ALL PERMUTATIONS OF STRINGS(ANAGRAMS)


EXAMPLE:- Below are the permutations of string ABC.
ABC ACB BAC BCA CBA CAB
1. USING BACKTRACKING
1 // Function to print permutations of string
2 // This function takes three parameters:
3 // 1. String
4 // 2. Starting index of the string
5 // 3. Ending index of the string.
6 void permute(string a, int l, int r)
7{
8 // Base case
9 if (l == r)
10 cout<<a<<endl;
11 else
12 {
13 // Permutations made
14 for (int i = l; i <= r; i++)
15 {
16
17 // Swapping done
18 swap(a[l], a[i]);
19
20 // Recursion called
21 permute(a, l+1, r);
22
23 //backtrack
24 swap(a[l], a[i]);
25 }
26 }
27 }
2. USING RECURSION
1 void permute(string s , string answer)
2{
3 if(s.length() == 0)
4 {
5 cout<<answer<<" ";
6 return;
7 }
8 for(int i=0 ; i<s.length() ; i++)
9 {
10 char ch = s[i];
11 string left_substr = s.substr(0,i);
12 string right_substr = s.substr(i+1);
13 string rest = left_substr + right_substr;
14 permute(rest , answer+ch);
15 }
16
17 }

7. TWO STRING WHETHER THEY ARE ANAGRAM OR NOT


EXAMPLE: - INPUT “abc” and “bac”
Output: yes
https://www.geeksforgeeks.org/check-whether-two-strings-are-anagram-of-each-
other/

8. REMOVE VOWELS
Input : welcome to geeksforgeeks
Output : wlcm t gksfrgks
Input : what is your name ?
Output : wht s yr nm ?
1 string remVowel(string str)
2{
3 regex r("[aeiouAEIOU]");
4
5 return regex_replace(str, r, "");
6}
7

9. MINIMUM PARENTHESIS
Given a string S of parentheses ‘(‘ or ‘)’ where, 0\leq len(S)\leq 1000 . The task is to
find a minimum number of parentheses ‘(‘ or ‘)’ (at any positions) we must add to
make the resulting parentheses string is valid.
Examples:

Input: str = "())"


Output: 1
One '(' is required at beginning.

Input: str = "((("


Output: 3
Three ')' is required at end.
1 int minParentheses(string p)
2{
3
4 // maintain balance of string
5 int bal = 0;
6 int ans = 0;
7
8 for (int i = 0; i < p.length(); ++i) {
9
10 bal += p[i] == '(' ? 1 : -1;
11
12 // It is guaranteed bal >= -1
13 if (bal == -1) {
14 ans += 1;
15 bal += 1;
16 }
17 }
18
19 return bal + ans;
20 }

10.GENERATE SUBSTRING
Input : abcd
Output : a
b
c
d
ab
bc
cd
abc
bcd
abcd
1 void subString(char str[], int n)
2{
3 // Pick starting point
4 for (int len = 1; len <= n; len++)
5 {
6 // Pick ending point
7 for (int i = 0; i <= n - len; i++)
8 {
9 // Print characters from current
10 // starting point to current ending
11 // point.
12 int j = i + len - 1;
13 for (int k = i; k <= j; k++)
14 cout << str[k];
15
16 cout << endl;
17 }
18 }
19 }

You might also like