This complete series of interviewing questions are divided into days for easy and better understanding. Follow the Day pattern and practice coding as much you can.
- Find last word count in a sentence: using string and char * using C++.
- Reverse string using C++,
- With temporary variable using C++.
- Without temporary variable using C++.
- Find vowels count in a string using C++.
- Find a word in a sentence using C++.
- Find given number is Palindrome using C++.
Find last word count in a sentence : using char * using C++.
#include <iostream>
using namespace std;
int findLenghtlastWord_Char(char *ptrSentence)
{
if (!ptrSentence)
return 0;
int i = strlen(ptrSentence);
int index = 0;
int count = 0;
while (i > 0)
{
if (ptrSentence[index] != ' ')
{
i--;
count++;
index++;
}
else
{
while (i > 0 && ptrSentence[index] == ' ')
{
i--;
index++;
if (i != 0)
count = 0;
}
}
}
return count;
}
Find last word count in a sentence: using string using C++.
int CountLenghtlastWord_String(string sentence)
{
int i = sentence.length();
int count = 0;
int index = 0;
while (i > 0)
{
if (sentence[index] != ' ')
{
index++;
count++;
i--;
}
else
{
while (i > 0 && sentence[index] == ' ')
{
index++;
i--;
if (i != 0)
count = 0;
}
}
}
return count;
}
Reverse string using temporary variable in C++.
char * reverseChar(char *ptrChar)
{
int len = strlen(ptrChar);
for (int i = 0, j= len -1; i<j; i++, j--)
{
char ch = ptrChar[i];
ptrChar[i] = ptrChar[j];
ptrChar[j] = ch;
}
return ptrChar;
}
Reverse string without using temporary variable in C++.
string reverseWithoutTemp(string strName, int start, int end)
{
while (start < end)
{
strName[start] ^= strName[end];
strName[end] ^= strName[start];
strName[start] ^= strName[end];
++start;
--end;
}
return strName;
}
Find vowels count in a string using C++.
int countVowels(char *sentence)
{
int len = strlen(sentence);
int count = 0;
int index = 0;
while (len > 0)
{
if (toupper(sentence[index]) == 'A' || toupper(sentence[index]) == 'E' ||
toupper(sentence[index]) == 'I' || toupper(sentence[index]) == 'O' || toupper(sentence[index]) == 'U')
{
count++;
}
len--;
index++;
}
return count;
}
Find a word in a sentence using C++.
bool findWordInSentence(char *sentence, char *key)
{
int len = strlen(sentence);
int keylen = strlen(key);
int count = 0;
int index = 0;
int key_index = 0;
while (len > 0)
{
if (sentence[index] == key[key_index])
{
count++;
index++;
key_index++;
len--;
if (keylen == count + 1)
{
return true;
}
}
else
{
count = 0;
index++;
key_index = 0;
len--;
}
}
return false;
}
Find given number is Palindrome using C++.
bool IsPalindromeString(string key)
{
int len = key.length();
int count = 0;
for (int i = 0, j = len - 1; i < j; i++, j--)
{
if (key[i] == key[j])
count++;
}
if (count == len / 2)
return true;
else
return false;
}
Main function for execution:
int main()
{
char sentence[] = "Hello my name is Mohammed Iliyas Patel ";
int length1 = findLenghtlastWord_Char(sentence);
int length2 = CountLenghtlastWord_String(sentence);
cout << "1. Last word count of string: '" << sentence << "' = " << length2 << endl;
char Name[] = "Iliyas";
reverseChar(Name);
string revStringName = reverseWithoutTemp(Name, 0, strlen(Name) - 1);
//reverse(revStringName.begin(), revStringName.end()); std function
cout << "2. Reverse of '" << Name << "' = "<< revStringName.c_str() << endl;
int count = countVowels(sentence);
cout << "3. Vowels in '" << sentence << "' : Count = " << count << endl;
char Key[] = "Iliyas";
bool found = findWordInSentence(sentence, Key);
cout << "4. Word : ' " << Key << " ' : In a sentence '" << sentence << " ' : " << found << endl;
string Pal = "abddba";
bool isPalindrome = IsPalindromeString(Pal);
cout << "5. Given String " << Pal.c_str() << " : Palindrome = " << isPalindrome << endl;
return 0;
}
OUTPUT:
1. Last word count of string: ‘Hello my name is Mohammed Iliyas Patel ‘ = 5
2. Reverse of ‘sayilI’ = Iliyas
3. Vowels in ‘Hello my name is Mohammed Iliyas Patel ‘ : Count = 13
4. Word : ‘ Iliyas ‘ : In a sentence ‘Hello my name is Mohammed Iliyas Patel ‘ : 1
5. Given String abddba : Palindrome = 1
Thanks 🙂