UP Board Solutions for Class 10 Computer Science Chapter 10 String Data Manipulation
String Data Manipulation Long Answer Type Questions (8 Marks)
Question 1.
What do you know about string function? Explain three-string functions with suitable examples.
Or
What is a string? Explain two string functions of your choice. (UP 2007, 08, 11, 12, 13)
Answer:
String Functions. String functions are those functions which handle string data. ‘C’ language uses a large set of useful string handling library functions. Three string functions are:
1. STRCPY( ): This function copies the contents of one string into another. The base addresses of the source and target strings should be supplied to this function.
Example:
void main() { char src[ ] = “Rajeev”; char tgt[20]; strcpy (tgt, src); printf (“Source string = %s”, src); printf (“Target string = %s”, tgt); } Output: Source string = Rajeev Target string = Rajeev
2. STRLWR(): This function converts upper case (A to Z) string to all lower case (a to z).
Example:
void main() { char STR[ ] = “RAJEEV”; printf (“Upper case = %s”, STR); strlwr(STR); printf(“lower case = %s”, STR); } Output: Upper case = RAJEEV Lower case = rajeev
3. STRUPR( ): This function converts lower case (a to z) string to upper case (A to Z) string:
Example:
void main() { char str[ ] = “rajeev”; prinft (“lower case = %S”, str); strupr (str); printf (“upper case = %S”, str); }
Output:
Lower case = rajeev
Upper case = RAJEEV
Question 2.
Explain GETS() and PUTS() function with example.
Answer:
GETS( ): In ‘C’ scanf( ) is not capable of receiving a multiword string, therefore, names such as “Rajeev Sharma” would be unacceptable. The function collects a string of characters terminated by a new line, the standard input.
PUTS(): The function puts() can display only one string at a time. Puts copies the null-terminated string to the standard output. Also on display a string, unlike printf(), puts() places the cursor on the next line.
Example:
void main() { char name [20]; printf (“enter your name"); gets (name); puts (“Good Morning !”); puts (name); }
Output:
Enter your name, Rajeev Sharma
Good Morning
Rajeev Sharma.
Question 3.
What is concatenation? Explain with example.
Or
Explain the mechanism of Concatenation of a new word in a string. (UP 2017)
Answer:
Concatenation: Concatenation means joining of two different strings to form one string. In ‘C’, streat function is used to do concatenation.
STRCAT(): This function appends or concatenates the source string at the end of the target string. For example, “Rajeev” and “Sharma” on concatenation would result in a string “Rajeev Sharma”. The length of the resulting string is strlen(dest) + strlen(src).
Example:
void main() { char src[ ] = “Rajeev”; char tgt[ ] = “Sharma”; strcat (tgt, src); printf (“\n source string = % s”, src); printf (“\n target string = % s”, tgt); }
Output:
source string = Rajeev
target string = Sharma Rajeev.
Question 4.
WAP to check for palindrome.
Answer:
Program:
#include<stdio.h> #include<conio.h> void main() { char str[20); char temp[20]; puts (“Enter any word”); gets (str); strcpy (temp, str); strrev (str); if (strcmp (str, temp) == 0) { printf (“it is a palindrome”); } else { printf (“it is not a palindrome”); } }
Question 5.
WAP to change a string from lower case to upper case.
Answer:
Program:
#include<stdio.h> #include<conio.h> #include void main() { char str [20]; puts (“enter a string in lower case”); gets (str); strupr (str); printf (“upper case = %S”, str); }
String Data Manipulation Short Answer Type Questions (4 Marks)
Question 1.
What is strlen in ‘C’ language? (UP 2011)
Answer:
STRLEN( ): This function calculates the length of a string. It returns the number of characters in a string, not counting the terminating null character. Its syntax is illustrated in the following program:
void main() { char N[ ] = “This is a book”; int len; len = strlen(N); printf (“The length of the string is %d”, len); }
Question 2.
What are the strings? (UP 2008, 18)
Answer:
Strings: Like a group of integers can be stored in an integer array, similarly a group of characters can be stored in a character array. Strings are single-dimensional arrays of type char. In ‘C’, a string is terminated by null character or ‘\0’. String constants are written in double-quotes.
For Example: **char name [ ] = {‘I’, ‘N’, ‘D’, ‘T’, ‘A’ , ‘\0’};
Each character in the array occupies one byte of memory and the last character is always ‘\0’.’\0′ is called null character. Note that ‘\0’ and ‘0’ are not the same. ASCII value of ‘\0’ is 0, whereas the ASCII value of ‘O’ is 48. Many string functions such as string length, string compare, string copy, string concatenate etc. are most commonly used functions.
Question 3.
What is the purpose of strcmp function?
Answer:
Strcmp function. This function compares two strings to find out whether they are the same or different. The string comparison starts with the first character in each string and continues with subsequent characters until the corresponding characters differ or until the end of the strings is reached. If the two strings are identical, strcmp () returns a value zero. If they are not, it returns the numeric difference between the ASCII values of the non-matching characters.
Example:
void main () { char str 1 [ ] = “Rajeev”; char str 2 [ ] = “Sharma”; int i, j, k; i = strcmp(str l, “Rajeev”); j = strcmp (str l, str 2); k = strcmp (str l, “Rajeev Sharma”); printf (“\n % d %d % d”, i, j, k); }
Output:
0 – 1 – 32
String Data Manipulation Very Short Answer Type Questions (2 Marks)
Question 1.
Which function is used to find the length of a string?
Answer:
strlen() function.
Question 2.
What is the name of the function which is used to copy a string into another?
Answer:
strcpy () function.
Question 3.
In ‘C’ language a string is terminated with what?
Answer:
In ‘C’ language a string is terminated by null character or ‘10’.
Question 4.
What is the ASCII value of ‘10’ and ‘0’?
Answer:
ASCII value of ‘10’ is 0. ASCII value of ‘0’ is 48.
Question 5.
The process of appending one string at the end of smother string is known as ………..
Answer:
Concatenation.
Question 6.
Function to check the time of the day. (UP 2012)
Answer:
time ( ).
String Data Manipulation Objective Type Questions (1 Mark)
There are four alternative answers for each part of the questions. Select the correct one and write in your answer book:
Question 1.
A variable name must start with:
(a) An alphabet
(b) A number
(c) $ symbol
(d) # sign
Answer:
(a) An alphabet
Question 2.
% is used for:
(a) String data type
(b) Character data type
(c) Numeric data type
(d) Float data type.
Answer:
(a) String data type
Question 3.
Which header file is used for sesmf () and print ()?
(a) stdio.h
(b) conio.h
(d) math.h
(d) Both (a) and (b).
Answer:
(b) conio.h
Question 4.
Which of the following is not a looping statement?
(a) for
(b) if
(c) while
(d) do-while.
Answer:
(b) if