1. What is a string in C?
Answer:
A string in C is an array of characters terminated by a null character ('\0'). It
is used to store a sequence of characters, such as words or sentences.
2. How do you declare a string in C?
Answer:
A string can be declared in C as an array of characters, like this:
char str[50];
or initialized with values:
char str[] = "Hello";
3. How do you find the length of a string in C?
Answer:
The length of a string can be found using the strlen() function:
#include <string.h>
int len = strlen(str);
strlen() returns the number of characters in the string excluding the null
terminator.
4. How can you copy one string into another in C?
Answer:
To copy a string in C, use the strcpy() function:
#include <string.h>
strcpy(str2, str1);
This copies the contents of str1 into str2.
5. How can you compare two strings in C?
Answer:
To compare two strings, use the strcmp() function:
#include <string.h>
if (strcmp(str1, str2) == 0) {
printf("Strings are equal");
} else {
printf("Strings are different");
}
strcmp() returns 0 if the strings are equal.
6. How do you concatenate two strings in C?
Answer:
To concatenate two strings, use the strcat() function:
#include <string.h>
strcat(str1, str2);
This appends the contents of str2 to the end of str1.
7. How do you convert a string to uppercase in C?
Answer:
To convert a string to uppercase, use the strupr() function (available in some
compilers) or iterate over each character and convert it manually:
#include <ctype.h>
for (int i = 0; str[i] != '\0'; i++) {
str[i] = toupper(str[i]);
}
8. How do you convert a string to lowercase in C?
Answer:
To convert a string to lowercase, use the strlwr() function (available in some
compilers) or manually convert each character:
#include <ctype.h>
for (int i = 0; str[i] != '\0'; i++) {
str[i] = tolower(str[i]);
}
9. How do you reverse a string in C?
Answer:
To reverse a string, you can use a loop or recursion. Here's an example using a
loop:
#include <string.h>
void reverseString(char str[]) {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
10. What is the difference between strcpy() and strncpy()?
Answer:
- strcpy() copies the entire string, including the null terminator, from the source
to the destination.
- strncpy() copies a specified number of characters, which can be less than the
length of the string.
Example:
strncpy(dest, source, 5); // Copies first 5 characters
11. How do you search for a substring within a string in C?
Answer:
You can use the strstr() function to search for a substring:
#include <string.h>
char *result = strstr(str, "sub");
if (result != NULL) {
printf("Substring found: %s\n", result);
} else {
printf("Substring not found\n");
}
12. How can you find the position of a character in a string in C?
Answer:
You can use the strchr() function to find the first occurrence of a character:
#include <string.h>
char *result = strchr(str, 'a');
if (result != NULL) {
printf("Character found at position: %ld\n", result - str);
} else {
printf("Character not found\n");
}
13. What is the null character ('\0') in C strings?
Answer:
The null character ('\0') is used to mark the end of a string. It signifies the
termination of a string, so functions like strlen(), strcpy(), and others know
where the string ends.
14. How do you trim spaces from the beginning and end of a string?
Answer:
To trim spaces, you can use a loop to shift the characters:
#include <ctype.h>
int start = 0, end = strlen(str) - 1;
while (isspace(str[start])) start++;
while (isspace(str[end])) end--;
for (int i = start; i <= end; i++) {
printf("%c", str[i]);
}
15. How can you find the frequency of a character in a string?
Answer:
You can iterate through the string and count occurrences of the character:
int count = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == 'a') {
count++;
}
}
printf("Character 'a' occurs %d times\n", count);
16. What is the strncmp() function used for?
Answer:
strncmp() compares the first n characters of two strings:
#include <string.h>
if (strncmp(str1, str2, 5) == 0) {
printf("First 5 characters are equal\n");
}
17. How do you copy a portion of a string in C?
Answer:
You can use the strncpy() function to copy a portion of a string:
#include <string.h>
strncpy(destination, source, n); // Copies first n characters from source to
destination
18. What is memcpy() and how is it different from strcpy()?
Answer:
memcpy() copies a specified number of bytes from one memory location to another,
while strcpy() is specifically for copying C strings (including the null
terminator). memcpy() does not consider the null terminator.
19. How do you concatenate strings with a specified limit in C?
Answer:
You can use strncat() to concatenate strings with a specified limit:
#include <string.h>
strncat(str1, str2, n); // Concatenates up to n characters from str2 to str1
20. How do you convert a string to an integer in C?
Answer:
You can use the atoi() function to convert a string to an integer:
#include <stdlib.h>
int num = atoi(str);
This converts the string str to an integer.