Practice questions on Strings
Last Updated :
03 Aug, 2022
String is an important topic from GATE exam point of view. We will discuss key points on strings as well different types of questions based on that. There are two ways to store strings as character array (char p[20]) or a pointer pointing to a string (char* s = “string”), both of which can be accessed as arrays. Assuming p as character array and s as pointer pointing to a string, following are the key points:
- The operator sizeof(p) will give the memory allocated to character array. However, sizeof(s) will give size of pointer which is independent of the string pointed by p.
- Each string is appended by a null character (‘\0’) which depicts end of the string.
- The length of string can be calculated using strlen() function. However, the function does not include null character ‘\0’ in the length. For example, strlen(s) will return 6.
- Single character from strings can be printed as:
printf(“%c”, s[0]);
printf(“%c”, p[1]);
- Complete string can be printed as:
printf(“%s”, s);
printf(“%s”, p);
- As p is a character array, its individual elements can be modified (p[i] = ‘c’). However, using s (a pointer to string), individual elements of string can’t be modified.
- As s is a pointer, it can be pointed to any other string as well (s =”string2”). However, using p, it is not possible.
Let us discuss some problems based on the concepts discussed:
Que - 1. What does the following fragment of C-program print?
char c[] = "GEEK2018";
char *p =c;
printf("%c,%c", *p,*(p+p[3]-p[1]));
- (A) G, 1
- (B) G, K
- (C) GEEK2018
- (D) None of the above
Solution: As given in the question, p points to character array c[] which can be represented as:
As p is a pointer of type character, *p will print ‘G’ Using pointer arithmetic, *(p+p[3]-p[1]) = *(p+75-69) (Using ascii values of K and E) = *(p+6) = 1(as we know p is holding the address of base string means 0th position string, let assume the address of string starts with 2000 so p+6 means the address of p(we are adding 6 in 2000 that means 2006, and in 2006 the "1"is stored that is why answer is 1). Therefore, the output will be G, 1.
Que - 2. Which of the following C code snippet is not valid?
- (A) char* p = “string1”; printf(“%c”, *++p);
- (B) char q[] = “string1”; printf(“%c”, *++q);
- (C) char* r = “string1”; printf(“%c”, r[1]);
- (D) None of the above
Solution: Option (A) is valid as p is a pointer pointing to character ‘s’ of “string1”. Using ++p, p will point to character ‘t’ in “string1”. Therefore, *++p will print ‘t’. Option (B) is invalid as q being base address of character array, ++q(increasing base address) is invalid. Option (C) is valid as r is a pointer pointing to character ‘s’ of “string1”. Therefore,
r[1] = *(r+1) = ‘t’ and it will print ‘t’.
Que - 3. Consider the following C program segment: (GATE CS 2004)
char p[20];
char *s = "string";
int length = strlen(s);
int i;
for (i = 0; i < length; i++)
p[i] = s[length — i];
printf("%s",p);
The output of the program is:
- (A) gnirts
- (B) gnirt
- (C) string
- (D) no output is printed
Solution: In the given code, p[20] is declared as a character array and s is a pointer pointing to a string. The length will be initialized to 6. In the first iteration of for loop (i = 0),
p[i] = s[6-0] and s[6] is ‘\0’ Therefore, p[0] becomes ‘\0’. As discussed, ‘\0’ means end of string. Therefore, nothing is printed as first character of string is ‘\0’.
Que - 4. What does the following fragment of C-program print?
char c[] = "GATE2011";
char *p =c;
printf("%s", p + p[3] - p[1]) ;
- (A) GATE2011
- (B) E2011
- (C) 2011
- (D) 011
Solution: As given in the question, p points to character array c[] which can be represented as:
As p is a pointer of type character, using pointer arithmetic, p + p[3] - p[1] = p + 69 - 65 (Using Ascii values of A and E) = p + 4 Now, p + 4 will point to 2, the string starting from 2 till ‘\0’ will be printed which is 2011.
Similar Reads
Python String Coding Practice Problems
This collection of Python string coding practice problems is designed to help you learn string manipulation.The following links contain coding problems where you need login first and then write code. Your code would be tested against expected output. You get points on the portal if your code's outpu
2 min read
Pattern of Strings
Given a string S of length N, find the pattern of the strings as shown below in the examples. Examples: Input: S = "Geek"Output: Geek, Gee, Ge, GExplanation: Decrease one character after each line Input: S = "G*g" Output: G*g, G*, GExplanation: Decrease one character after each line Using two Nested
5 min read
string erase in C++
The string erase() function is a built-in function of the string class that is used to erase the whole or part of the string, shortening its length. The function provides different ways to delete a portion of a string based on either an index position or a range of characters or delete the whole str
4 min read
Strings in C
A String in C programming is a sequence of characters terminated with a null character '\0'. The C String is work as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character '\0'.DeclarationDeclaring a string in C i
5 min read
C# String Properties
In C#, a String is an array of characters. The string class represents the text as a series of Unicode characters. It provides various properties and methods so that it becomes easy to work with strings. There are two properties in the string class:Chars[Int32]: Used to get the Char object at a spec
4 min read
std::string::compare() in C++
The string::compare() function in C++ is used to compare a string or the part of string with another string or substring. It is the member function of std::string class defined inside <string> header file. In this article, we will learn how to use string::compare() in C++.The different ways to
4 min read
Commonly Asked Data Structure Interview Questions on Strings
Strings are essential data structures used to represent sequences of characters and are frequently encountered in coding interviews. Questions often focus on string manipulation techniques such as searching, concatenation, reversal, and substring extraction. Understanding key algorithms like pattern
4 min read
C++ string class and its applications
In C++ we can store string by one of the two ways â C style stringsstring class (discussed in this post) In this post, the second method is discussed. string class is part of C++ library that supports a lot much functionality over C style strings. C++ string class internally uses char array to store
6 min read
Left Rotation of a String
Given a string s and an integer d, the task is to left rotate the string by d positions.Examples:Input: s = "GeeksforGeeks", d = 2Output: "eksforGeeksGe" Explanation: After the first rotation, string s becomes "eeksforGeeksG" and after the second rotation, it becomes "eksforGeeksGe".Input: s = "qwer
15+ min read
TCS National Qualifier 2 Coding Question.
You are given a string and your task is to print the frequency of each character. Direction to Solve Problem: 1. Take string from STDIN. aaaabbBcddee2. Get all different characters in given string using set(). set ={a, b, B, c, d, e} # unordered set3. Iterate for different characters ( len(set )) be
3 min read