# 17 Lecture

## String Handling

String handling is an important concept in C programming, as it involves working with strings of characters. String handling functions allow us to manipulate and work with strings in various ways, such as copying, concatenating, and comparing st

## Important Mcq's Midterm & Finalterm Prepration Past papers included

1. What is the standard library function to compare two strings? a) strcat() b) strcmp() c) strlen() d) strcpy() Answer: b) strcmp()

2. What is the standard library function to find the length of a string? a) strcat() b) strcmp() c) strlen() d) strcpy() Answer: c) strlen()

3. Which function can be used to convert a string to a floating-point number? a) atof() b) atoi() c) atol() d) strtof() Answer: a) atof()

4. Which function is used to concatenate two strings? a) strcat() b) strcmp() c) strlen() d) strcpy() Answer: a) strcat()

5. Which function is used to copy one string to another? a) strcat() b) strcmp() c) strlen() d) strcpy() Answer: d) strcpy()

6. What is the maximum length of a string in C? a) 255 characters b) 256 characters c) 512 characters d) There is no maximum length Answer: d) There is no maximum length

7. Which function can be used to find the first occurrence of a character in a string? a) strchr() b) strrchr() c) strstr() d) strcmp() Answer: a) strchr()

8. What is the standard library function to convert a string to an integer? a) atoi() b) atof() c) atol() d) strtoi() Answer: a) atoi()

9. Which function can be used to extract a substring from a string? a) strtok() b) strspn() c) strcspn() d) strncpy() Answer: d) strncpy()

10. What is the standard library function to convert an integer to a string? a) itoa() b) atoi() c) ltoa() d) strtoi() Answer: a) itoa()

## Subjective Short Notes Midterm & Finalterm Prepration Past papers included

1. What is a string in C programming? How is it different from a character array? Ans: A string is a sequence of characters terminated by a null character '\0'. It is a built-in data type in C language. A character array is a collection of characters. The difference between a string and a character array is that a string is terminated by a null character whereas a character array is not.

2. How can you input a string in C programming? Ans: We can input a string in C programming using the 'gets' function or the 'scanf' function with '%s' format specifier.

3. How can you output a string in C programming? Ans: We can output a string in C programming using the 'puts' function or the 'printf' function with '%s' format specifier.

4. How can you find the length of a string in C programming? Ans: We can find the length of a string in C programming using the 'strlen' function.

5. How can you concatenate two strings in C programming? Ans: We can concatenate two strings in C programming using the 'strcat' function.

6. How can you compare two strings in C programming? Ans: We can compare two strings in C programming using the 'strcmp' function.

7. How can you copy one string into another in C programming? Ans: We can copy one string into another in C programming using the 'strcpy' function.

8. What is a substring in a string? How can you extract a substring from a string in C programming? Ans: A substring is a portion of a string. We can extract a substring from a string in C programming using the 'strncpy' function or the 'strstr' function.

9. How can you convert a string to an integer in C programming? Ans: We can convert a string to an integer in C programming using the 'atoi' function.

10. How can you convert an integer to a string in C programming? Ans: We can convert an integer to a string in C programming using the 'sprintf' function.

String handling is a crucial concept in programming. In C language, strings are represented as character arrays, where the last character is always a null character '\0'. String functions help manipulate the contents of a string. The standard string library provides several functions such as strlen(), strcpy(), strcat(), strcmp(), etc. to perform various operations on strings. Functions like strlen() calculate the length of the string, while strcpy() copies a string from one location to another. Similarly, strcat() concatenates two strings, and strcmp() compares two strings. C also provides a feature to handle strings using pointers. Pointers can be used to access the characters of the string and manipulate them. For example, using a pointer, we can traverse a string and change its characters as well. String handling is widely used in various programming applications, such as database handling, text processing, etc. It is essential to have a good understanding of string handling to write efficient programs. Some of the common string operations that programmers perform include copying a string, concatenating two or more strings, searching for a substring within a string, and manipulating the case of a string. With the help of string functions, these operations can be easily performed in C programming.