Top 15 string Interview Questions in C, might ask by the interviewer.

string Interview questions in C

For the interviewer, string questions are fascinating generally interviewer ask the questions related to the string algorithm. So here I am creating a collection of some important string questions that might be asked by your interviewer.

Remove all duplicates from a given string

Suppose you have a string “silence is a source of great strength” when we will remove the duplicate character from the string then the output will be “silenc aourfgth“.

Steps to check the duplicate character in a given string:

 

1. Initialize the required variable

pString = “silence is a source of great strength” ;/* input string */

startIndex = 0 ;/* index of next character in input string */

resultIndex = 0; /* index of next character in resultant string */

binTable[255] = {0}; /* Binary table to check the duplicate character */

2. Create a loop that runs until the end of the string (till the null character).

3. We know that every character has the ASCII value, so mark the index of binTable for the unique ASCII value,

for example,

Suppose if input string is pString ,
pString = “silence is a source of great strength” ; /* input string */

Get the character from the string ( pString )

binTableIndex = *(pString + startIndex);

If the character is unique then,

marked binTable for startIndex,
*(pString + startIndex) = MARK_USED;

Copy the *(pString + startIndex) character in resultant string.
*(pString + resultIndex) = *(pString + startIndex);

increment the resultIndex,
resultIndex++;

Increment the startIndex,
startIndex++;

If the character is not unique then,

Only increment the startIndex,
startIndex++;

Again go to read next character until not get the null character.

Assigned the null character in the last of the resultant string to remove the extra character.*(pString+resultIndex) = ‘\0’;

 

For the better understanding see the below code,

 

Reverse words in a given string

This question generally asks by an interviewer, in this question you need to reverse the words of the string, for example, if your input string is “How are you”, the output will be “you are How”.

Steps to reverse words in a given string

  • First, you need to reverse the individual words, for example,
    If an input is “How are you”, the output will be “woH era uoy”.
  • Now, reverse the whole string and you will get “you are How”.

 

For the better understanding see the below code,

 

Searching for a pattern in a given string

Sometimes interviewer asks the question to search a pattern in a given string. There are a lot of algorithms to find the pattern, later we will discuss all the algorithm in detail. Here I am using the Naive algorithm (not a smart algorithm) to search a Pattern.

Suppose a given source string is src[0..n-1] and a pattern is pat[0..m-1]. Here to search the pattern for a given string, we need to slide the pattern string (pat) over source string (src) one by one and check for the match. If the match is found, then slides by 1 again to check for subsequent matches.

Example,
Input: src[] = “How are you”
Patteren: pat[] = “are”
Output: Pattern found at index 4

 

For the better understanding see the below code,

 

If you want to learn more about the c language, here 10 Free days (up to 200 minutes) C video course for you.

Your free trial is waiting.

 

Write your own atoi()

The atoi() is a c library function used to convert a numeric string to his integer value.

Steps to create own atoi().

The atoi() only convert a numeric string to their integer value, so check the validity of the string. If any non-numeric character comes, the conversion will be stopped.

For more detail see this article:  create own atoi().

Subtract 48 (ASCII value of 0) from the string character to get the actual value and perform some arithmetical operation.

for example,

If the numeric string is “124”, we know that ASCII value of ‘1’, ‘2, and ‘4’ is 49, 50 and 52 respectively. So if we subtract 48 from these numeric characters we will get the actual numeric value 1,2 and 4.

 

For the better understanding see the below code,

 

Reverse a string in c without using a library function

In the interview generally, Interviewer asked the question to reverse a string without the using the c library function or maybe they can mention more condition, it totally depends on the interviewer.

For more detail see this article: Reverse a string in c

Algorithm:

  • Calculate the length (Len) of string.
  • Initialize the indexes of the array.
    Start = 0, End = Len-1
  • In a loop, swap the value of pszData[Start] with pszData[End].
  • Change the indexes’ of the array as follows.
    Start = start +1; End = end – 1

 

For the better understanding see the below code,

 

As per suggestion, here I am giving 10 important string questions for the practice. If you have any doubt or you faced any problem, then just hit the comment button and share your problem with us. We will definitely try to solve your problem.

  1. How to check if String is Palindrome?
  2. How to find all permutations of String?
  3. A Program to check if strings are rotations of each other or not?
  4. How to count number of vowels and consonants in a String?
  5. How to count occurrence of a given character in String?
  6. How to return highest occurred character in a String?
  7. How to reverse String in c using Recursion?
  8. How to replace each given character to another?
  9. Left Rotation and Right Rotation of a String?
  10. calculate sum of all numbers present in a string in c?

 



One comment

Leave a Reply