Skip to content

Commit f6353cc

Browse files
feat: Solutions to C Strings problem sets 2-6
1 parent aa2875f commit f6353cc

File tree

5 files changed

+243
-0
lines changed

5 files changed

+243
-0
lines changed

Unit 2/Exercises/C Strings/2.c

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Write a program in C to find the length of a string without using library function.
3+
4+
Test Data:
5+
Input the string: w3resource.com
6+
7+
Expected Output:
8+
Length of the string is: 15
9+
*/
10+
11+
#include <stdio.h>
12+
13+
14+
int main() {
15+
char str[100];
16+
int length = 0;
17+
18+
// Input the string
19+
printf("Input the string: ");
20+
fgets(str, sizeof(str), stdin);
21+
22+
// Find the length of the string
23+
while (str[length] != '\0') {
24+
length++;
25+
}
26+
27+
// Output the length of the string. Subtracting 1 to ignore the newline character added by fgets
28+
printf("Length of the string is: %d\n", length - 1);
29+
30+
return 0;
31+
}

Unit 2/Exercises/C Strings/3.c

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Write a program in C to separate the individual characters from a string.
3+
4+
Test Data:
5+
Input the string: w3resource.com
6+
7+
8+
Expected Output:
9+
The characters of the string are:
10+
w 3 r e s o u r c e . c o m
11+
*/
12+
13+
#include <stdio.h>
14+
15+
int main() {
16+
char input[100]; // Array to hold the input string
17+
int i; // Loop counter
18+
19+
// Prompt the user for input
20+
printf("Input the string: ");
21+
22+
// Read the input string, including spaces, and ensure it doesn't exceed the array size
23+
fgets(input, sizeof(input), stdin);
24+
25+
// Remove the newline character if present
26+
for (i = 0; input[i] != '\0'; i++) {
27+
if (input[i] == '\n') {
28+
29+
// Replace newline with null terminator
30+
input[i] = '\0';
31+
32+
break;
33+
}
34+
}
35+
36+
// Display the characters of the string
37+
printf("\n");
38+
printf("The characters of the string are:\n");
39+
40+
for (i = 0; input[i] != '\0'; i++) {
41+
// Print each character followed by two spaces
42+
printf("%c ", input[i]);
43+
}
44+
45+
// Add a newline at the end of the output for better formatting
46+
printf("\n");
47+
48+
return 0;
49+
}

Unit 2/Exercises/C Strings/4.c

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Write a program in C to print individual characters of string in reverse order.
3+
4+
Test Data:
5+
Input the string: w3resource.com
6+
7+
Expected Output:
8+
The characters of the string in reverse are:
9+
m o c . e c r u o s e r 3 w
10+
*/
11+
12+
13+
#include <stdio.h>
14+
#include <string.h>
15+
16+
int main() {
17+
char input[100]; // Array to hold the input string
18+
int length; // Variable to store the length of the string
19+
20+
// Prompt the user for input
21+
printf("Input the string: ");
22+
fgets(input, sizeof(input), stdin);
23+
24+
// Remove the newline character if present
25+
length = strlen(input);
26+
27+
if (input[length - 1] == '\n') {
28+
// Replace newline with null terminator
29+
input[length - 1] = '\0';
30+
31+
// Adjust the length
32+
length--;
33+
}
34+
35+
// Display the characters of the string in reverse
36+
printf("\nThe characters of the string in reverse are:\n");
37+
38+
for (int i = length - 1; i >= 0; i--) {
39+
// Print each character followed by two spaces
40+
printf("%c ", input[i]);
41+
}
42+
43+
// Add a newline at the end for proper formatting
44+
printf("\n");
45+
46+
return 0;
47+
}

Unit 2/Exercises/C Strings/5.c

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Write a program in C to count the total number of words in a string.
3+
4+
Test Data:
5+
Input the string: This is w3resource.com
6+
7+
Expected Output:
8+
Total number of words in the string is: 3
9+
*/
10+
11+
#include <stdio.h>
12+
#include <string.h>
13+
14+
int main()
15+
{
16+
char input[100]; // Array to store the input string
17+
int wordCount = 0; // Counter for the total number of words
18+
int inWord = 0; // Flag to check if currently inside a word
19+
20+
// Prompt user for input & Read the input string including spaces
21+
printf("Input the string: ");
22+
fgets(input, sizeof(input), stdin);
23+
24+
// Loop through each character in the input string
25+
for (int i = 0; input[i] != '\0'; i++) {
26+
27+
// Check for whitespace characters (space, newline, or tab)
28+
if (input[i] == ' ' || input[i] == '\n' || input[i] == '\t') {
29+
// Mark as outside a word
30+
inWord = 0;
31+
32+
} else if (inWord == 0) {
33+
// A non-whitespace character marks the start of a new word
34+
inWord = 1; // Mark as inside a word
35+
wordCount++; // Increment the word count
36+
}
37+
}
38+
39+
// Display the result
40+
printf("Total number of words in the string is: %d\n", wordCount);
41+
42+
return 0;
43+
}

Unit 2/Exercises/C Strings/6.c

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Write a program in C to compare two strings without using string library functions.
3+
4+
Test Data:
5+
Check the length of two strings:
6+
--------------------------------
7+
Input the 1st string: aabbcc
8+
Input the 2nd string: abcdef
9+
String1: aabbcc
10+
String2: abcdef
11+
12+
Expected Output:
13+
Strings are not equal.
14+
15+
Check the length of two strings:
16+
--------------------------------
17+
Input the 1st string: aabbcc
18+
Input the 2nd string: aabbcc
19+
String1: aabbcc
20+
String2: aabbcc
21+
22+
Expected Output:
23+
Strings are equal.
24+
*/
25+
26+
#include <stdio.h>
27+
28+
int compareStrings(char str1[], char str2[]) {
29+
int i = 0;
30+
31+
// Compare characters one by one
32+
while (str1[i] != '\0' && str2[i] != '\0') {
33+
if (str1[i] != str2[i]) {
34+
return 0; // Strings are not equal
35+
}
36+
i++;
37+
}
38+
39+
// Check if both strings reached the end
40+
if (str1[i] == '\0' && str2[i] == '\0') {
41+
return 1; // Strings are equal
42+
} else {
43+
return 0; // Strings are not equal
44+
}
45+
}
46+
47+
int main() {
48+
char str1[100], str2[100];
49+
50+
printf("Check the length of two strings:\n");
51+
printf("--------------------------------\n");
52+
53+
// Input first string
54+
printf("Input the 1st string: ");
55+
scanf("%s", str1);
56+
57+
// Input second string
58+
printf("Input the 2nd string: ");
59+
scanf("%s", str2);
60+
61+
// Display the strings
62+
printf("String1: %s\n", str1);
63+
printf("String2: %s\n", str2);
64+
65+
// Compare strings and display result
66+
if (compareStrings(str1, str2)) {
67+
printf("Strings are equal.\n");
68+
} else {
69+
printf("Strings are not equal.\n");
70+
}
71+
72+
return 0;
73+
}

0 commit comments

Comments
 (0)