Skip to content

Commit

Permalink
Merge pull request #330 from mokshmahajan2004/moksh
Browse files Browse the repository at this point in the history
Added Strings Folder in DSA
  • Loading branch information
sudhanshu-77 authored Oct 30, 2024
2 parents 60a5d37 + 30d69f5 commit 6f8c27b
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
37 changes: 37 additions & 0 deletions DSA/Strings/Length& Reverse String.cpp
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;
}
52 changes: 52 additions & 0 deletions DSA/Strings/Palindrome.cpp
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;
}
54 changes: 54 additions & 0 deletions DSA/Strings/Replace Strings.cpp
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;
}

0 comments on commit 6f8c27b

Please sign in to comment.