Skip to content

Asking to add some string basic interview quetions for builiding confidence #2823

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions strings/interviewQuetions/quetion1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// to check whether a string is a palindrome
// a palindrome can be considered as a string readed backwards and forwards same.
#include<bits/stdc++.h>
using namespace std;

int main(){
//declaration of a string
string result = "hi";
//creating a copy by storing the string in another string.
string check = result;
//reversing the string
int start = 0;
int end = result.size() - 1;
while(start<=end){
swap(result[start], result[end]);
start++;
end--;
}
if(result==check){
cout<<"String is a Palindrome"<<endl;
}
else{
cout<<"String is not a Palindrome"<<endl;
}
return 0;
}
27 changes: 27 additions & 0 deletions strings/interviewQuetions/quetion10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Return the maximum occurring elements.
#include<bits/stdc++.h>
using namespace std;

int main(){
string result = "Aaaabhirajjjjj";
unordered_map<char, int> freq;

// Convert all characters to lowercase and count frequency
for(int i = 0; i < result.size(); i++){
char lowerChar = tolower(result[i]);
freq[lowerChar]++;
}

// Find the maximum occurring character
char maxChar = result[0];
int maxCount = 0;
for(auto& pair : freq){
if(pair.second > maxCount){
maxCount = pair.second;
maxChar = pair.first;
}
}

cout << "The maximum occurring character is: " << maxChar << " with " << maxCount << " occurrences." << endl;
return 0;
}
25 changes: 25 additions & 0 deletions strings/interviewQuetions/quetion2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//count number of vowels,consonants and spaces in a string
#include<bits/stdc++.h>
using namespace std;

int main(){
string testCase = "Abhiraj Tiwari";
int vowels = 0, consonants = 0, spaces = 0;
unordered_map<char, bool> alphabets = {{'a', true}, {'e', true}, {'i', true}, {'o', true}, {'u', true}, {'A', true}, {'E', true}, {'I', true}, {'O', true}, {'U', true}};
for(int i=0;i<testCase.size();i++){
if(alphabets.find(testCase[i]) != alphabets.end()){
vowels++;
}
else if(testCase[i] == ' '){
spaces++;
}
else{
consonants++;
}
}
cout<<vowels<<endl;
cout<<consonants<<endl;
cout<<spaces<<endl;

return 0;
}
21 changes: 21 additions & 0 deletions strings/interviewQuetions/quetion3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//remove all vowels and spaces from string
#include<bits/stdc++.h>
using namespace std;

int main(){
string result = "This is a sample string with vowels.";
unordered_map<char, bool> alphabets = {{'a', true}, {'e', true}, {'i', true}, {'o', true}, {'u', true}, {'A', true}, {'E', true}, {'I', true}, {'O', true}, {'U', true}};
for(int i=0; i<result.size();){
if(alphabets.find(result[i]) != alphabets.end()){
result.erase(i, 1);
}
else if(result[i] == ' '){
result.erase(i,1);
}
else {
i++;
}
}
cout << result << endl;
return 0;
}
17 changes: 17 additions & 0 deletions strings/interviewQuetions/quetion4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//remove brackets from the string
#include<bits/stdc++.h>
using namespace std;

int main(){
string result = "(Abhiraj)";
for(int i=0;i<result.size();i++){
if(result[i] == '(' || result[i] == ')'){
result.erase(i,1);
}
else{
i++;
}
}
cout<< result << endl;
return 0;
}
12 changes: 12 additions & 0 deletions strings/interviewQuetions/quetion5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include<bits/stdc++.h>
using namespace std;

int main(){
string numbers = "12345";
int sum = 0;
for(int i=0;i<numbers.size();i++){
sum+=numbers[i]-'0';
}
cout<<sum;
return 0;
}
18 changes: 18 additions & 0 deletions strings/interviewQuetions/quetion6.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//capitalize first and last letter
#include<bits/stdc++.h>
using namespace std;

int main(){
string result = "abhiraj";
for(int i=0;i<result.size();i++){
if (i == 0 || i == result.size() - 1) {
if (islower(result[i])) {
result[i] = toupper(result[i]);
} else if (isupper(result[i])) {
result[i] = tolower(result[i]);
}
}
}
cout<<result;
return 0;
}
17 changes: 17 additions & 0 deletions strings/interviewQuetions/quetion7.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//to check repeating character

#include<bits/stdc++.h>
using namespace std;

int main(){
string result = "Abhiraj";
sort(result.begin(),result.end());

for(int i=0;i<result.size()-1;i++){
if(result[i]==result[i+1]){
cout<<"this character is repeating:"<<" "<<result[i]<<endl;
}
}

return 0;
}
17 changes: 17 additions & 0 deletions strings/interviewQuetions/quetion8.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//check if 2 strings are anagram of each other

#include<bits/stdc++.h>
using namespace std;

int main(){
string result1 = "mug";
string result2 = "gum";
sort(result1.begin(), result1.end());
sort(result2.begin(), result2.end());
if(result1 == result2){
cout<<"Both strings are Anagrams"<<endl;
} else {
cout<<"Strings are not Anagrams"<<endl;
}
return 0;
}
17 changes: 17 additions & 0 deletions strings/interviewQuetions/quetion9.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//check if 2 strings match where one string contains a wildcard character
#include<bits/stdc++.h>
using namespace std;

int main(){
unordered_map<char, bool> alphabets = {{'*', true}, {'@', true}, {'#', true}, {'$', true}};
string result1 = "Abhiraj";
string result2 = "@Abhiraj";
for(int i=0;i<result1.size();i++){
for(int j=0;j<result2.size();j++){
if(alphabets.find(result1[i]) != alphabets.end() || result1[i] == result2[j]){
cout<<"Both strings are equal";
}
}
}
return 0;
}
151 changes: 151 additions & 0 deletions strings/interviewQuetions/strings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# STL and Strings in C++

## Introduction
The Standard Template Library (STL) in C++ provides various useful algorithms and containers to manage data. When working with strings, STL offers a powerful set of functions, containers, and algorithms that allow for efficient manipulation of text.

In this guide, we will discuss how to use STL features to work with `std::string` in C++, focusing on common operations such as iteration, search, modification, and transformation.

---

## Table of Contents
1. [What is `std::string`?](#what-is-stdstring)
2. [Basic Operations on Strings](#basic-operations-on-strings)
3. [Iterating Over Strings](#iterating-over-strings)
4. [String Modifications](#string-modifications)
5. [Search Operations](#search-operations)
6. [Common STL Algorithms with Strings](#common-stl-algorithms-with-strings)
7. [Conclusion](#conclusion)

---

## What is `std::string`?

`std::string` is part of the STL, which provides a flexible and powerful way to work with character sequences. Unlike traditional C-style strings (`char` arrays), `std::string` is a class that provides dynamic memory management, making it easier to work with strings of varying lengths.

### Example:
```cpp
#include <iostream>
#include <string>

int main() {
std::string str = "Hello, STL!";
std::cout << str << std::endl; // Output: Hello, STL!
return 0;
}
```
### Basic Operations on Strings
## String Length
The size() or length() function returns the number of characters in a string.
```cpp
std::string str = "C++ STL";
std::cout << "Length: " << str.length() << std::endl; // Output: 7
```
## Concatenation
You can concatenate two strings using the + operator.
```cpp
std::string str1 = "Hello, ";
std::string str2 = "World!";
std::string result = str1 + str2;
std::cout << result << std::endl; // Output: Hello, World!
```
## SubStrings
The substr() function extracts a substring from a given string.
```cpp
std::string str = "C++ Programming";
std::string sub = str.substr(4, 11); // Extracts "Programming"
std::cout << sub << std::endl;
```
<hr>

### Iterating Over Strings
You can use different iteration techniques to loop through the characters of a string.
## 1. Using for Loop
```cpp
std::string str = "STL Example";
for (int i = 0; i < str.size(); ++i) {
std::cout << str[i] << " ";
}
```
## 2. Using Range-based for Loop
```cpp
for (char c : str) {
std::cout << c << " ";
}
```
## 3. Using Iterators
```cpp
for (auto it = str.begin(); it != str.end(); ++it) {
std::cout << *it << " ";
}
```
<hr>

### String Modifications
## 1. Inserting Characters or Strings
The insert() function can add characters or strings at a specified position.
```cpp
std::string str = "Hello!";
str.insert(5, ", World"); // Inserts ", World" at position 5
std::cout << str << std::endl; // Output: Hello, World!
```
## 2. Removing Characters or Substrings
The erase() function removes characters or substrings from the string.
```cpp
std::string str = "abcdef";
str.erase(2, 3); // Removes 3 characters starting from position 2
std::cout << str << std::endl; // Output: abf
```
## 3. Replacing Part of a String
The replace() function allows replacing parts of the string.
```cpp
std::string str = "Good Morning!";
str.replace(5, 7, "Evening"); // Replaces "Morning" with "Evening"
std::cout << str << std::endl; // Output: Good Evening!
```

### Search Operations
## 1. Finding Substrings
The find() function locates a substring within the string.
```cpp
std::string str = "C++ is fun";
size_t pos = str.find("fun");
if (pos != std::string::npos) {
std::cout << "'fun' found at position " << pos << std::endl;
}
```
## 2. Reverse Find
The rfind() function searches for the last occurrence of a substring.
```cpp
std::string str = "one two one three";
size_t pos = str.rfind("one");
std::cout << "Last occurrence of 'one' is at: " << pos << std::endl; // Output: 8
```
<hr>

### Common STL Algorithms with Strings
## 1. Sorting Characters
Using std::sort() to sort the characters of a string.
```cpp
std::string str = "dbca";
std::sort(str.begin(), str.end());
std::cout << str << std::endl; // Output: abcd
```
## 2. Counting Occurrences
You can use std::count() to count the occurrences of a character in a string.
```cpp
int count = std::count(str.begin(), str.end(), 'a');
std::cout << "'a' appears " << count << " times" << std::endl;
```
## 3. Transforming Case
Using std::transform() to convert all characters to uppercase or lowercase.
```cpp
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
std::cout << str << std::endl; // Output: ABCD
```
<hr>

### Conclusion
The std::string class in STL offers a wide range of functionality for efficient string manipulation. Combining std::string with STL algorithms and iterators makes it a powerful tool for handling text in C++. With the examples and functions discussed in this guide, you should be able to perform most common string operations in your C++ projects.

### References
https://www.geeksforgeeks.org/strings-in-cpp/