In the following exercises, you need to get strings from the user. Use the following code to get a string, and remove the newline character.
...
#include <string.h>
...
char string[100];
fgets(string, sizeof(string), stdin);
// use the following to get rid of the newline character
// that gets attached to the end of the input string
string[strcspn(string, "\n")] = '\0';
...
Question 01
Write a C program to concatenate two strings entered by the user without using strcat().
Answer 01
#include <stdio.h>
#include <string.h>
void concatenateStrings(char str1[], char str2[], char result[]) {
int i = 0, j = 0;
// Copy first string into result
while (str1[i] != '\0') {
result[i] = str1[i];
i++;
}
// Append second string to result
while (str2[j] != '\0') {
result[i] = str2[j];
i++;
j++;
}
result[i] = '\0'; // Null-terminate the result string
}
int main() {
char str1[100], str2[100], result[200];
printf("Enter the first string: ");
fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = '\0'; // Remove newline character
printf("Enter the second string: ");
fgets(str2, sizeof(str2), stdin);
str2[strcspn(str2, "\n")] = '\0'; // Remove newline character
concatenateStrings(str1, str2, result);
printf("Concatenated string: %s\n", result);
return 0;
}
Question 02
Write a C program that calculates the length of a string entered by the user without using the strlen function.
Answer 02
#include <stdio.h>
#include <string.h>
int stringLength(char str[]) {
int length = 0;
while (str[length] != '\0') {
length++;
}
return length;
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0'; // Remove newline character
int length = stringLength(str);
printf("The length of the string is: %d\n", length);
return 0;
}
Question 03
Write a C program that converts a string entered by the user to uppercase. Use the function toupper() from header file ctype.h to convert a character to uppercase.
...
#include <ctype.h>
...
// The value of ch after running the following statement is 'A'
char ch = toupper('a');
...
Answer 03
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = 0; // Remove newline character
for (int i = 0; str[i] != '\0'; i++) {
str[i] = toupper(str[i]); // Convert to uppercase
}
printf("String in uppercase: %s\n", str);
return 0;
}
Question 04
Write a C program that finds the first occurrence of a character in a string entered by the user.
Answer 04
#include <stdio.h>
#include <string.h>
int findFirstOccurrence(char str[], char ch) {
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == ch) {
return i; // Return the index of the first occurrence
}
}
return -1; // Return -1 if the character is not found
}
int main() {
char str[100], ch;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0'; // Remove newline character
printf("Enter the character to find: ");
scanf("%c", &ch);
int index = findFirstOccurrence(str, ch);
if (index != -1) {
printf("The first occurrence of '%c' is at index %d.\n", ch, index);
} else {
printf("Character not found in the string.\n");
}
return 0;
}
Question 05
Write a C program that counts the number of times a character appears in a string entered by the user.
Answer 05
#include <stdio.h>
#include <string.h>
int countOccurrences(char str[], char ch) {
int count = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == ch) {
count++;
}
}
return count;
}
int main() {
char str[100], ch;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0'; // Remove newline character
printf("Enter the character to count: ");
scanf("%c", &ch);
int count = countOccurrences(str, ch);
printf("The character '%c' appears %d times in the string.\n", ch, count);
return 0;
}
Last modified: Saturday, 23 November 2024, 6:23 PM