Interview Programming Questions and Answers in C — Part 2

Interview Programming Questions and Answers in C

Q. W.A.P to find the length of a string.
A.

#include <stdio.h>int main()
{
char ch[20];
int count = 0, i;

printf("Enter any string :: ");
scanf("%s", ch);

for(i = 0; ch[i] != '\0'; i++)
{
count++;
}

printf("Length of the String = %d", count);

return 0;
}

Q. W.A.P to check the input is an alphabet or not.
A.

#include <stdio.h>void main()
{
char ch;

printf("Enter a character :: ");
scanf("%c", &ch);

if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("%c is an alphabet", ch);
}
else
{
printf("%c is not an alphabet", ch);
}
}

Q. W.A.P to convert a lowercase character to uppercase.
A.

#include <stdio.h>void convertLowercaseToUppercase();int main()
{
convertLowercaseToUppercase();
return 0;
}
void convertLowercaseToUppercase()
{
char ch;
printf("Please Enter any alphabet :: ");
scanf("%c", &ch);
if(ch >= 97 && ch <= 122)
{
ch = ch - 32;
printf("Uppercase of Entered character is = %c", ch);
}
else
{
printf("Please Enter Valid Alphabet.");
}
}

Q. W.A.P to convert an uppercase character to lowercase.
A.

#include <stdio.h>void convertUppercaseToLowercase();int main()
{
convertUppercaseToLowercase();
return 0;
}
void convertUppercaseToLowercase()
{
char ch;
printf("Please Enter any alphabet :: ");
scanf("%c", &ch);
if(ch >= 65 && ch <= 90)
{
ch = ch + 32;
printf("Lowercase of Entered character is = %c", ch);
}
else
{
printf("Please Enter Valid Alphabet.");
}
}
Algorithm
ASCII value of 'A' is 65 while 'a' is 97. Difference between them is 97 – 65 = 32
So if we will add 32 in the ASCII value of 'A' then it will be 'a' and if will we subtract 32 in ASCII value of 'a' it will be 'A'. It is true for all alphabets.
In general rule:
Upper case character = Lower case character – 32
Lower case character = Upper case character + 32

Q. W.A.P to convert lowercase string to uppercase string.
A.

#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100void convertLowercaseStringToUppercase();int main(void)
{
convertLowercaseStringToUppercase();
return 0;
}
void convertLowercaseStringToUppercase()
{
char str[MAX_SIZE];
int i;
printf("Please Enter Any String in Lowercase :: ");
scanf("%s", str);
printf("The String is = %s \n", str); for(i = 0; str[i] != '\0'; i++)
{
if(str[i] >= 'a' && str[i] <= 'z')
{
str[i] = str[i] - 32;
}
}
printf("Uppercase of Entered String is = %s \n", str);
}

Q. W.A.P to convert uppercase string to lowercase string.
A.

#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100void convertUppercaseStringToLowercase();int main(void)
{
convertUppercaseStringToLowercase();
return 0;
}
void convertUppercaseStringToLowercase()
{
char str[MAX_SIZE];
int i;
printf("Please Enter Any String in Uppercase :: ");
scanf("%s", str);
printf("The String is = %s \n", str); for(i = 0; str[i] != '\0'; i++)
{
if(str[i] >= 'A' && str[i] <= 'Z')
{
str[i] = str[i] + 32;
}
}
printf("Lowercase of Entered String is = %s \n", str);}

Q. W.A.P to concatenate two strings.
A.

#include <stdio.h>int main()
{
char strOne[50], strTwo[50], strCon[100], i, count = 0;

printf("Enter first string :: ");
//fgets(strOne, sizeof(strOne), stdin);
scanf("%s", strOne);

printf("Enter second string: ");
//fgets(strTwo, sizeof(strTwo), stdin);
scanf("%s", strTwo);

for(i = 0; strOne[i] != '\0'; i++)
{
strCon[i] = strOne[i];
count++;
}

for(i = 0; strTwo[i] != '\0'; i++)
{
strCon[count] = strTwo[i];
count++;
}

strCon[count] = '\0';

printf("String 1 = %s \n", strOne);
printf("String 2 = %s \n", strTwo);
printf("Concatenated String = %s", strCon);

return 0;
}

Q. W.A.P to find the frequency of character in a string.
A.

#include <stdio.h>int main() 
{
char str[1000], ch;
int count = 0;
printf("Enter a string :: ");
fgets(str, sizeof(str), stdin);
printf("Enter a character to find its frequency :: ");
scanf("%c", &ch);
for (int i = 0; str[i] != '\0'; i++)
{
if (ch == str[i])
{
count++;
}
}
printf("Frequency of %c = %d", ch, count);

return 0;
}

Q. W.A.P to find the reverse of a string.
A. Eg: ABCD to DCBA

#include <stdio.h>
#include <string.h>
int main()
{
char str[100], temp;
int i = 0, j = 0;

printf("Enter the string :: ");
fgets(str, sizeof(str), stdin);
//scanf("%s", str);

j = strlen(str) - 1;

while(i < j)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}

printf("Reverse string is = %s", str);

return 0;
}

Q. W.A.P to check whether two strings are an anagram of each other.
A. Two string will be anagram to each other, if and only if they contain the same number of characters, the order of characters doesn’t matter. i.e., If two strings are anagram to each other, then one string can be rearranged to form the other string. For Example, abc and cba are anagrams. creative and reactive are also an anagram.

#include <stdio.h>
#include <string.h>
int main()
{
char str1[20], str2[20];
int len, len1, len2, i, j, found=0, notFound=0;

printf("Enter first string :: ");
fgets(str1, sizeof(str1), stdin);
//scanf("%s", str1);

printf("Enter second string :: ");
fgets(str2, sizeof(str2), stdin);
//scanf("%s", str2);

len1 = strlen(str1);
len2 = strlen(str2);

if(len1 == len2)
{
len = len1;
for(i = 0; i < len; i++)
{
found = 0;
for(j = 0; j < len; j++)
{
if(str1[i] == str2[j])
{
found = 1;
break;
}
}
if(found == 0)
{
notFound = 1;
break;
}
}

if(notFound == 1)
{
printf("Strings are not Anagram to each other.");
}
else
{
printf("Strings are Anagram.");
}
}
else
{
printf("Two strings must have same no of character to be Anagram.");
}

return 0;
}

Q. W.A.P to remove spaces from String.
A.

#include <stdio.h>
#include <string.h>
int main()
{
char str1[1000], str2[1000];
int i = 0, j = 0;
printf("Enter the string :: ");
fgets(str1, strlen(str1), stdin);
while(str1[i] != '\0')
{
if(str1[i] != ' ')
{
str2[j] = str1[i];
j++;
}

i++;
}

str2[j] = '\0';

printf("The string is :: %s", str2);

return 0;
}

Q. W.A.P to sort string in ascending order.
A.

#include <stdio.h>
#include <string.h>
int main()
{
char str[100], chTemp;
int i, j, len;

printf("Enter any string :: ");
scanf("%s", str);

len = strlen(str);

for(i = 0; i < len; i++)
{
for(j = 0; j < (len-1); j++)
{
if(str[j] > str[j+1])
{
chTemp = str[j];
str[j] = str[j+1];
str[j+1] = chTemp;
}
}
}
printf("Same string in ascending order :: %s", str);

return 0;
}

Q. W.A.P to sort string in descending order.
A.

#include <stdio.h>
#include <string.h>
int main()
{
char str[100], chTemp;
int i, j, len;

printf("Enter any string :: ");
scanf("%s", str);

len = strlen(str);

for(i = 0; i < len; i++)
{
for(j = 0; j < (len-1); j++)
{
if(str[j] < str[j+1])
{
chTemp = str[j];
str[j] = str[j+1];
str[j+1] = chTemp;
}
}
}
printf("Same string in descending order :: %s", str);

return 0;
}

.

“The best way to learn a new programming language is by writing programs in it.” — Dennis Ritchie

Thank you for reading! If you liked this article, please clap to get this article seen by more people.
Please follow me on Medium by clicking Follow.
I’m also active on LinkedIn, Twitter, and GitHub.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store