-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #330 from mokshmahajan2004/moksh
Added Strings Folder in DSA
- Loading branch information
Showing
3 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <iostream> | ||
using namespace std; | ||
#include <bits\stdc++.h> | ||
void reverse(char name[], int n) | ||
{ | ||
int s = 0; | ||
int e = n - 1; | ||
while (s < e) | ||
{ | ||
swap(name[s++], name[e--]); | ||
} | ||
} | ||
int getLength(char name[]) | ||
{ | ||
int count = 0; | ||
for (int i = 0; name[i] != '\0'; i++) | ||
{ | ||
count++; | ||
} | ||
return count; | ||
} | ||
|
||
int main() | ||
{ | ||
char name[20]; | ||
cout << "Enter your Name " << endl; | ||
cin >> name; | ||
cout << "Your name is "; | ||
cout << name << endl; | ||
cout << "Length: " << getLength(name) << endl; | ||
int len = getLength(name); | ||
reverse(name, len); | ||
cout << "Your name is "; | ||
cout << name << endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include <iostream> | ||
using namespace std; | ||
char toLowerCase(char ch) | ||
{ | ||
if (ch >= 'a' && ch <= 'z') | ||
return ch; | ||
else | ||
{ | ||
char temp = ch - 'A' + 'a'; | ||
return temp; | ||
} | ||
} | ||
bool checkPalindrome(char a[], int n) | ||
{ | ||
int s = 0; | ||
int e = n - 1; | ||
while (s <= e) | ||
{ | ||
if (toLowerCase(a[s]) != toLowerCase(a[e])) | ||
{ | ||
return 0; | ||
} | ||
else | ||
{ | ||
s++; | ||
e--; | ||
} | ||
} | ||
return 1; | ||
} | ||
int getLength(char name[]) | ||
{ | ||
int count = 0; | ||
for (int i = 0; name[i] != '\0'; i++) | ||
{ | ||
count++; | ||
} | ||
return count; | ||
} | ||
|
||
int main() | ||
{ | ||
char name[20]; | ||
cout << "Enter your Name " << endl; | ||
cin >> name; | ||
cout << "Your name is "; | ||
cout << name << endl; | ||
cout << "Length: " << getLength(name) << endl; | ||
int len = getLength(name); | ||
cout << "Check Palindrome or Not: " << checkPalindrome(name, len); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include <iostream> | ||
using namespace std; | ||
char getMax(string s) | ||
{ | ||
int arr[26] = {0}; | ||
// Create an array of count of characters | ||
for (int i = 0; i < s.length(); i++) | ||
{ | ||
char ch = s[i]; | ||
// lowercase | ||
int number = 0; | ||
number = ch - 'a'; | ||
arr[number]++; | ||
} | ||
int maxi = -1, ans = 0; | ||
for (int i = 0; i < 26; i++) | ||
{ | ||
if (maxi < arr[i]) | ||
{ | ||
ans = i; | ||
maxi = arr[i]; | ||
} | ||
} | ||
char finalAns = 'a' + ans; | ||
return finalAns; | ||
} | ||
string replaceSpaces(string s) | ||
{ | ||
string temp = ""; | ||
for (int i = 0; i < s.length(); i++) | ||
{ | ||
if (s[i] == ' ') | ||
{ | ||
temp.push_back('@'); | ||
temp.push_back('4'); | ||
temp.push_back('0'); | ||
} | ||
else | ||
{ | ||
temp.push_back(s[i]); | ||
} | ||
} | ||
return temp; | ||
} | ||
int main() | ||
{ | ||
char s[50]; | ||
// cin >> s; | ||
// cout << getMax(s) << endl; | ||
cout << "Enter the string " << endl; | ||
cin.getline(s, 50); | ||
cout << "String without spaces is " << replaceSpaces(s); | ||
return 0; | ||
} |