0% found this document useful (0 votes)
6 views15 pages

String Manipulation Functions in C++

Uploaded by

aaksixueiwksj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views15 pages

String Manipulation Functions in C++

Uploaded by

aaksixueiwksj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

string ReplaceeWordsInString(string S1, string WordReplacee, string ReplaceeTo)

{
int pos = [Link](WordReplacee);
while (pos != std::string::npos)
{
S1 = [Link](pos, [Link](), ReplaceeTo);
pos = [Link](WordReplacee);
}

return S1;
}

string Remove_All_Punctuations(string word)


{
string Result;
for (char &ch :word)
{
if (!ispunct(ch))
{
Result += ch;
}
}
return Result;
}

string ReverseWordInString(string str)


{
vector<string>vString;
string s2 = "";
vString = SplitString(str, " ");
//vector<string>::iterator iter = [Link]();
cout << "Tokens = " << [Link]() << endl;
int size = [Link]();
for (int i = 0; i < size; i++)
{
s2 += [Link]() + " ";
vString.pop_back();
}
return s2;
}

string JoinString(string ArrS[100], int size, string delim)


{
string Words;
for (short i = 0; i < size; i++)
{
Words += ArrS[i] + delim;

}
//this way to delete the last dilemeter
return [Link](0, [Link]() - [Link]());
}

string JoinString(vector <string> st, string delim)


{

string Words;
for (string& w : st)
{
Words += w + delim;
}
//this way to delete the last dilemeter
return [Link](0, [Link]() - [Link]());
}

string TrimLeft(string s1)


{
for (int i = 0; i < [Link](); i++)
{
if (s1[i] != ' ')
return [Link](i, [Link]() - 1);
}
return "";
}

string TrimRight(string s1)


{
for (int i = [Link](); i >= 0; i--)
{
if (s1[i - 1] != ' ')
return [Link](0, i);
}
return "";
}

string Trim(string s1)


{
return TrimLeft(TrimRight(s1));
}

vector <string> SplitString(string st, string delim)


{
vector <string>Tokens;
int pos = 0;
string word;

while ((pos = [Link](delim)) != std::string::npos)


{
word = [Link](0, pos);
if (word != "")
{
Tokens.push_back(word);
}
[Link](0, pos + [Link]());
}

if (![Link]())
{
Tokens.push_back(st);
}
return Tokens;
}
void CountWordsInString(string st)
{
string delim = " ";
int count = 0;
short pos = 0;
string sword;

while ((pos = [Link](delim)) != std::string::npos)


{
sword = [Link](0, pos);
if (sword != "")
{
count++;
}
[Link](0, pos + [Link]());
}

if (sword != "")
{
count++;
}
cout << "The number of words in your string is : " << count << endl;
}

void CountWord(string St)


{
int count = 0;
bool isNewWord = false;
bool firstWord = true;
for (short i = 0; i < [Link](); i++)
{
if (firstWord)
{
count++;
firstWord = false;
}
if (St[i] != ' ' && isNewWord)
{
count++;
}
isNewWord = (St[i] == ' ' ? true : false);
}
cout << "The number of words in your string is : " << count << endl;
}

void PrintEachWord(string St)


{
bool isNewWord = false;
for (short i = 0; i < [Link](); i++)
{
if (St[i] != ' ' && isNewWord)
{
cout << endl;
}
cout << St[i];
isNewWord = (St[i] == ' ' ? true : false);
}
cout << endl;
}

bool IsVowel(char cha)


{
if (cha == 'a' || cha == 'o' || cha == 'u' || cha == 'e' || cha == 'i' ||
cha == 'A' || cha == 'O' || cha == 'U' || cha == 'E' || cha == 'I')
{
return 1;
}
}

void PrintallVowels(string str)


{
cout << "Vowels in string are: ";
for (int i = 0; i < [Link](); i++)
{

if (IsVowel(str[i]))
{
cout << str[i] << " ";
}
}
cout << "\n";
}

void CountLetter(string st, char ch)


{

int countU = 0;
int countL = 0;

for (int i = 0; i < [Link](); i++)


{
if (toupper(ch) == st[i])
{
countU++;
}
else if (tolower(ch) == st[i])
countL++;

}
cout << "Letter '" << ch << "' Count " << countL << endl;
cout << "Letter '" << char(tolower(ch)) << "' or " << char(toupper(ch)) << "'
Count " << countL + countU << endl;
}

void CapitalLettersCount(string St)


{
int Capital = 0, Small = 0;
for (char& ch : St) {
if (isupper(ch))
Capital++;

else if (islower(ch))
Small++;
}
cout << "Capital Letters Count = " << Capital << "\n";
cout << "Small Letters Count = " << Small << "\n";
}

void CalculateTheSumOfEachRow(int arr[3][3], short Row, short Colom)


{

cout << "The following are the sum of each row in the random matrix:\n";
for (int R = 0; R < Row; R++)
{
int SumOfEachRow = 0;
for (int C = 0; C < Colom; C++)
{
SumOfEachRow += arr[R][C];
//cout << setw(3) << arr[R][C] << " ";
}
cout << "The Sum Of Row " << R + 1 << " = " << SumOfEachRow << endl;
}

void SumAtArray1D(int Array[3])


{
for (int i = 0; i < 3; i++)
{
cout << "The Sum Of Row " << i + 1 << " = " << Array[i] << endl;
}
cout << "\n";
}

void CalculateTheSumOfEachRowinArray(int arr[3][3], short Row, short Colom)


{

int Array[3];
cout << "The following are the sum of each row in the random matrix:\n";
for (int R = 0; R < Row; R++)
{
int SumOfEachRow = 0;
for (int C = 0; C < Colom; C++)
{
SumOfEachRow += arr[R][C];
}

Array[R] = SumOfEachRow;
}

SumAtArray1D(Array);

int RandomNumber(int From, int To)


{
//Function to generate a random number
int randNum = rand() % (To - From + 1) + From;
return randNum;
}

void FillArrayWithRandomNumber(int arr[3][3], short Row, short Colom)


{

for (int i = 0; i < Row; i++)


{
for (int r = 0; r < Colom; r++)
{
arr[i][r] = RandomNumber(0, 9);
//cout << "the number are inserted is " << arr[i][r]<<endl;
}
}

void PrintArray2D(int arr[3][3], short Row, short Colom)


{
for (int i = 0; i < Row; i++)
{
for (int r = 0; r < Colom; r++)
{
cout << setw(3) << arr[i][r] << " ";
}
cout << endl;
}

void CalculateTheSumOfEachRow(int arr[3][3], short Row, short Colom)


{

cout << "The following are the sum of each row in the random matrix:\n";
for (int R = 0; R < Row; R++)
{
int SumOfEachRow = 0;
for (int C = 0; C < Colom; C++)
{
SumOfEachRow += arr[R][C];
//cout << setw(3) << arr[R][C] << " ";
}
cout << "The Sum Of Row " << R + 1 << " = " << SumOfEachRow << endl;
}

void SumAtArray1D(int Array[3])


{
for (int i = 0; i < 3; i++)
{
cout << "The Sum Of Row " << i + 1 << " = " << Array[i] << endl;
}
cout << "\n";
}

void CalculateTheSumOfEachRowinArray(int arr[3][3], short Row, short Colom)


{

int Array[3];
cout << "The following are the sum of each row in the random matrix:\n";
for (int R = 0; R < Row; R++)
{
int SumOfEachRow = 0;
for (int C = 0; C < Colom; C++)
{
SumOfEachRow += arr[R][C];
}

Array[R] = SumOfEachRow;
}

SumAtArray1D(Array);

void invertcase(char& chara)


{
islower(chara) ? chara = toupper(chara) : chara = tolower(chara);
}

void ReverseLetter(string& St)


{
for (char& chara : St) {
invertcase(chara);
}
}

string ReadString()
{
string str;
cout << "Please Enter the String?\n";
getline(cin, str);
return str;
}

char ReadChar()
{
char ch;
cout << "Please Enter a Character?\n";
cin >> ch;
return ch;
}

void PrintString(const string& St)


{
cout << St << endl;
}

void ConvertToTitleCase(string& St)


{
bool isFirstletter = true;
for (char& chara : St) {
if (chara != ' ' && isFirstletter)
{
chara = toupper(chara);
}
else
chara = tolower(chara);
isFirstletter = (chara == ' ' ? true : false);
}
}

void PrintFirstLettersofEachWord(string St)


{

bool isFirstletter = true;


for (short i = 0; i < [Link](); i++)
{

if (St[i] != ' ' && isFirstletter)


{
cout << St[i] << endl;
};
isFirstletter = (St[i] == ' ' ? true : false);
}

int PrintFibonacciRec(int number, short Prev1, short Prev2)


{
int curr;
if (number >= 1) {
curr = Prev1 + Prev2;
cout << Prev1 << " ";
Prev1 = Prev2;
Prev2 = curr;
number--;
return PrintFibonacciRec(number, Prev1, Prev2);
}
cout << endl;
}

void PrintFibonacci(int number)


{
short Prev1 = 1, Prev2 = 1, curr;

for (short i = 0; i < number; i++)


{
curr = Prev1 + Prev2;
cout << Prev1 << " ";
Prev1 = Prev2;
Prev2 = curr;
}
cout << endl;
}

bool Palindromeornot(int Arr[3], short size)


{
int left = 0, right = size - 1;

while (left < right)


{
if (Arr[left] != Arr[right])
{
return false;
}
left++;
right--;
}
return true;
}

void CheckIfTheMatrixArePlanOrNOT(int Arr[3][3], short row, short col)


{
// ÇáÊÍÞÞ ãä ßá ÕÝ
for (int R = 0; R < row; R++) {
if (Palindromeornot(Arr[R], col) != 1) { // ÊãÑíÑ ÇáÕÝ ßãÕÝæÝÉ 1D
cout << "The Matrix is NOT Palindrome.\n";
break;
}
}
cout << "The Matrix is Palindrome.\n";

void MinNumberInMatrix(int Arr[3][3], short Row, int Col)


{
int MinNum = Arr[0][0];
for (int R = 0; R < Row; R++)
{
for (int C = 0; C < Col; C++)
{
if (Arr[R][C] < MinNum)
{

MinNum = Arr[R][C];
}
}
}
cout << "The Min Number in :" << MinNum << endl;
}

void MaxNumberInMatrix(int Arr[3][3], short Row, int Col)


{
int MaxNum = Arr[0][0];
for (int R = 0; R < Row; R++)
{
for (int C = 0; C < Col; C++)
{
if (Arr[R][C] > MaxNum)
{

MaxNum = Arr[R][C];
}
}
}
cout << "The Max Number in :" << MaxNum << endl;
}
void PrintIntersectedNumber(int Arr[3][3], int Arr2[3][3], short Row, int Col)
{
cout << "The Intersected Number are:\n";
for (int R = 0; R < Row; R++)
{
for (int C = 0; C < Col; C++)
{
if (IsNumberInMatrix(Arr, Arr2[R][C], 3, 3))
{
cout << Arr[R][C] << " ";
}
}
}
cout << "\n";
}

short CountNumberInMatrix(int Matrix1[3][3], int Number, short Rows, short Cols)


{
short NumberCount = 0;
for (short i = 0; i < Rows; i++)
{
for (short j = 0; j < Cols; j++)
{
if (Matrix1[i][j] == Number)
{
NumberCount++;
};
}
}
return NumberCount;
}

bool IsSparseMatrix(int Matrix1[3][3], short Rows, short Cols)


{
short MatrixSize = Rows * Cols;
return (CountNumberInMatrix(Matrix1, 0, 3, 3) >= ceil((float)MatrixSize /
2));
}

bool IsNumberInMatrix(int Arr[3][3], int number, short row, short col)


{

for (short R = 0; R < row; R++)


{
for (short C = 0; C < col; C++)
{
if (number == Arr[R][C])
{
return true;
}
}
}
return false;
}
int RandomNumber(int From, int To)
{
//Function to generate a random number
int randNum = rand() % (To - From + 1) + From;
return randNum;
}

void CalculateTheSumOfEachColom(int arrC[3][3], short Row, short Colom)


{

cout << "The following are the sum of each colom in the random matrix:\n";
for (int R = 0; R < Row; R++)
{
int SumOfEachColom = 0;
for (int C = 0; C < Colom; C++)
{
SumOfEachColom += arrC[C][R];

}
cout << "The Sum Of Colom " << R + 1 << " = " << SumOfEachColom <<
endl;
}

void PrintArray(int Array[3], short row)


{
for (int i = 0; i < row; i++)
{
cout << "The Sum Of Row " << i + 1 << " = " << Array[i] << endl;
}
cout << "\n";
}

void CalculateTheSumOfEachColominArray(int arrC[3][3], short Row, short Colom)


{
int ArSumC[3];
cout << "The following are the sum of each colom in the random matrix:\n";
for (int R = 0; R < Row; R++)
{
int SumOfEachColom = 0;
for (int C = 0; C < Colom; C++)
{
SumOfEachColom += arrC[C][R];

}
ArSumC[R] = SumOfEachColom;
//cout << "The Sum Of Colom " << R + 1 << " = " << SumOfEachColom <<
endl;
}
PrintArray(ArSumC, Row);
}

void FillArrayWithOrderedNumbers(int arr[3][3], short Row, short Colom)


{
int Counter = 1;
for (short i = 0; i < Row; i++)
{
for (short r = 0; r < Colom; r++)
{
arr[i][r] = Counter++;
}
}
}

void TransposeArray(int Arr[3][3], int TraArr[3][3], short Row, short Colom)


{
for (short R = 0; R < Row; R++)
{
for (short C = 0; C < Colom; C++)
{
TraArr[R][C] = Arr[C][R];
}
}
}

void Multiply2Array(int Arr1[3][3], int Arr2[3][3], int results[3][3], short Row,


short Colom)
{
for (short R = 0; R < Row; R++)
{
for (short C = 0; C < Colom; C++)
{
results[R][C] = Arr1[R][C] * Arr2[R][C];
}
}
}

void PrintTheRowMiddle(int Arr[3][3], short Row, short Colom)


{
short MiddleRow = Row / 2;
cout << "Middle Row of matrix \n";
for (short R = 0; R < Row; R++)
{
printf(" %0*d ", 2, Arr[MiddleRow][R]);
//cout << setw(3) << Arr[MiddleRow][R];
}
cout << "\n";
}

void PrintTheColomMiddle(int Arr[3][3], short Row, short Colom)


{
short MiddleCol = Colom / 2;
for (short C = 0; C < Colom; C++)
{
printf(" %0*d ", 2, Arr[C][MiddleCol]);
//cout << setw(3) << Arr[C][MiddleCol];
}
cout << "\n";
}

int SumOfMatrix(int Arr[3][3], short row, short col)


{
int Sum = 0;
for (short r = 0; r < row; r++)
{
for (short c = 0; c < col; c++)
{
Sum += Arr[r][c];
}
}
return Sum;
}

bool IsTypicalMatrix(int Arr1[3][3], int Arr2[3][3], short row, short col)


{

for (short r = 0; r < row; r++)


{
for (short c = 0; c < col; c++)
{
if (Arr1[r][c] != Arr2[r][c])
{
return false;
}
}
}
return true;
}

void CheckTheMatrixTypical(int Arr1[3][3], int Arr2[3][3], short row, short col)


{
if (IsTypicalMatrix(Arr1, Arr2, row, col))
{
cout << "Yes: Matrix are Typical. \n";
}
else
cout << "No: Matrix are NOT Typical. \n";

bool AreIdentity(int Arr[3][3], short row, short col)


{
for (short r = 0; r < row; r++)
{
for (short c = 0; c < col; c++)
{
if (r == c && Arr[r][c] != 1 || r != c && Arr[r][c] != 0)
return false;

}
}
return true;
}

void checkIfTheMatrixIdentityorNOT(int Arr[3][3], short row, short col)


{
if (AreIdentity(Arr, row, col) == true) {
cout << "The given matrix are Identity Matrix \n";
}
else
cout << "The given matrix are NOT Identity Matrix \n";
}

bool AreScalar(int Arr[3][3], short row, short col)


{
int checkScalar = Arr[0][0];
for (short r = 0; r < row; r++)
{
for (short c = 0; c < col; c++)
{
if (r == c && Arr[r][c] != checkScalar || r != c && Arr[r][c] !=
0)
return false;

}
}
return true;
}

void checkIfTheMatrixScalarorNOT(int Arr[3][3], short row, short col)


{
if (AreScalar(Arr, row, col)) {
cout << "The given matrix are Scalar Matrix \n";
}
else
cout << "The given matrix are NOT Scalar Matrix \n";
}

void ClaculatorTheNumber(int Arr[3][3], short row, short col)


{
int number, count = 0;
cout << "Enter the number to count in matrix? "; cin >> number;

for (short r = 0; r < row; r++)


{
for (int c = 0; c < col; c++)
{
if (number == Arr[r][c])
{
count++;
}
}
}
cout << "Number " << number << " count in matrix is " << count << "\n";

void checktheMatrixSparce(int Arr[3][3], short row, short col)


{
int count0 = 0;
for (short r = 0; r < row; r++)
{
for (short c = 0; c < col; c++)
{
if (Arr[r][c] == 0)
{
count0++;
}
}
}
if (count0 > (row * col / 2))
cout << "Yes: it's Sperse\n";
else
cout << "No: it's NOT Sperse\n";

short CountNumberInMatrix(int Matrix1[3][3], int Number, short Rows, short Cols)


{
short NumberCount = 0;
for (short i = 0; i < Rows; i++)
{
for (short j = 0; j < Cols; j++)
{
if (Matrix1[i][j] == Number)
{
NumberCount++;
};
}
}
return NumberCount;
}

bool IsSparseMatrix(int Matrix1[3][3], short Rows, short Cols)


{
short MatrixSize = Rows * Cols;
return (CountNumberInMatrix(Matrix1, 0, 3, 3) >= ceil((float)MatrixSize /
2));
}

bool IsNumberInMatrix(int Arr[3][3], int number, short row, short col)


{

for (short R = 0; R < row; R++)


{
for (short C = 0; C < col; C++)
{
if (number == Arr[R][C])
{
return true;
}
}
}
return false;
}

You might also like