String Practice Questions
1. What are the limitations of using getchar and scanf functions for reading
strings.
2. Compare the working of the following functions: a)strcpy and strncpy
b)strcat and strncat c)strcmp and strncmp
3. Why does strcmp return a number that’s less than, equal to ,greater than
zero ?Also does the exact return value have any significance?
4. What will be the value of the string str after the following statements have
been executed.
5. Write a program which will read a string and rewrite it in the alphabetic
order. For example the word STRING should be written as GINRST.
6. Write a program, which reads your name from the keyboard and outputs a list
of ASCII codes, which represent your name.
7. Fill in the blanks
a) We can use conversion specification ______________ in scanf to read a line
of text.
b) The ____________ function is used to determine the length of a string.
c)The _____________ string manipulation function determines if a character is
contained in a string.
8. What is the output of C program with strings?
9. What will be the value of the string s1 after the following statements have
been executed.
10. What is the output?
11.What will be the output of the following C code?
#include<stdio.h>
int main()
{
char *s= "hello";
char *p = s;
printf("%c %c", 1[p], s[1]);
}
12. What is the output?
#include<stdio.h>
#include<string.h>
int main()
{
char *str = "hello world";
char strc[] = "good morning india\n";
strcpy(strc, str);
printf("%s\n", strc);
return 0;
}
13. What will be the output of the following C code?
#include<stdio.h>
#include<string.h>
int main()
{
char str1[15];
char str2[15];
int mat;
strcpy(str1, "abcdef");
strcpy(str2, "ABCDEF");
mat= strncmp(str1, str2, 4);
if(mat< 0)
printf("str1 is not greater than str2");
else if(mat> 0)
printf("str2 is is not greater than str1");
else
printf("both are equal");
}
14. What is the output of following program?
#include<stdio.h>
void swap(char *str1, char *str2)
{
char *temp = str1;
str1 = str2;
str2 = temp;
}
int main()
{
char *str1 = "Java";
char *str2 = "Python";
swap(str1, str2);
printf("str1 is %s, str2 is %s", str1, str2);
return 0;
}
15. What will be the output of the following C code?
#include<stdio.h>
#include<string.h>
int main()
{
char *str = "hello, world\n";
printf("%d", strlen(str));
return 0;
}
16. What will be the output of the following C code?
#include <stdio.h>
#include<string.h>
int main()
{
char str[11] = "hello";
char *str1 = "world";
strcat(str, str1);
printf("%s %d", str, str[10]);
}
17. The______ function returns the number of characters that are present before
the terminating null character.
18. Predict the output.
#include <stdio.h>
int fun(char *str1)
{
char *str2 = str1;
while(*++str1);
return (str1-str2);
}
int main()
{
char *str = "snowwhite";
printf("%d", fun(str));
return 0;
}
18. What is the output?
#include<stdio.h>
int main()
{
char c[] = "GATE2011";
char *p =c;
printf("%s", p + p[3] - p[1]) ;
}
19. What is the output?
#include<stdio.h>
int main()
{
char str[] = "snow white";
printf("%s %s %s\n", &str[5], &5[str], str+5);
printf("%c %c %c\n", *(str+6), str[6], 6[str]);
return 0;
}
20. What is the output?
#include <stdio.h>
int main()
{
char arr[] = "snowwhite";
printf("%s", arr+4);
return 0;
}
21. Predict the output.
#include<stdio.h>
#include<string.h>
int main()
{
char p[] = "snowwhite";
char t;
int i, j;
for(i=0,j=strlen(p); i<j; i++)
{
t = p[i];
p[i] = p[j-i];
p[j-i] = t;
}
printf("%s", p);
return 0;
}
21.Program to find the count of given character in a given string.
22. WAP to input a string and print the Square of all the Numbers in a String.
Take care of special cases. Input: wha4ts12ap1p and Output: 16 1 4 1
23. Write a function to find the position of First occurrence of the input
character in a given string.
24. Write a function which takes two string arguments and find the Cartesian
product of passed strings. Input: str1: abc str2: ab and Output: aa, ab, ba, bb, ca,
cb
25. WAP to find the sum of all substrings of a string representing a number.
Input : num = "1234” Output : 1670 Sum = 1+2 + 3 + 4 + 12 + 23 + 34 + 123 +
234 + 1234 = 1670
26. Write a function to reverse a string using recursion and test the function
27. Write a program to delete the characters from string1 which are present in
string2. input: string1: whatsapp string2: wat output: hspp
28. Write a program to find if two strings are anagrams. Anagrams: Same set of
letters with different arrangements. Number of times a letter present in one
string must be equal to the number of times the letter is present on another
string