C Problem Solving
What is Problem Solving?
You now know a lot of C: variables, conditions, loops, arrays, strings and functions. The next step is to use them to solve problems.
When you get a new problem, it can be hard to know where to start. Most programmers solve problems in four steps:
- Understand the problem
- Plan the solution
- Write the code
- Test the code
In this chapter, we will use these four steps to solve one problem, from start to finish.
The Problem
Write a program that counts the vowels (the letters a, e, i, o and u) in a text.
Step 1: Understand the Problem
Before you write any code, make sure you know two things:
- What is the input? A text, like
"Hello World". - What is the output? A number: how many vowels the text has.
Then solve one example by hand. In "Hello World", the vowels are e, o and o, so the answer is 3.
If you can solve the problem by hand, you know what the program must do. Later, you can use your answer to check that the program is right.
Step 2: Plan the Solution
Write down the steps in plain words, before you write any C code. This is often called pseudocode (code written in plain words, that no computer can run):
count = 0
for each character in the text:
if the character is a, e, i, o or u:
add 1 to count
print count
This is exactly what you did by hand: you looked at one letter at a time, and counted the vowels.
Step 3: Write the Code
Now turn each line of the plan into C code:
| Plan | C code |
|---|---|
| count = 0 | int count = 0; |
| for each character in the text | for (int i = 0; i < strlen(text); i++) |
| if the character is a, e, i, o or u | if (c == 'a' || c == 'e' || ...) |
| add 1 to count | count++; |
| print count | printf("Vowels: %d\n", count); |
Put together, it looks like this:
Example
#include <stdio.h>
#include <string.h>
int main() {
char text[] = "Hello World";
int count = 0;
for (int i = 0; i < strlen(text); i++) {
char c = text[i];
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
count++;
}
}
printf("Vowels: %d\n", count);
return 0;
}
Result:
Vowels: 3
Try it Yourself »
The program prints Vowels: 3, the same answer you got by hand.
Step 4: Test the Code
One correct answer does not mean that the program always works. Test it with different texts, and compare the result with what you expect:
| Text | Expected | The program says |
|---|---|---|
| "Hello World" | 3 | 3 |
| "bcd" | 0 | 0 |
| "" (empty) | 0 | 0 |
| "APPLE" | 2 | 0 (wrong) |
The last test fails. The program only checks for lowercase vowels, so 'A' and 'E' are not counted.
This is why testing is important: you find the mistakes before the users of your program do.
Fix and Test Again
To fix it, use the tolower() function from <ctype.h>. It turns each character into lowercase before we check it.
We also put the solution in a function, so we can test all the texts in one run:
Example
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int countVowels(char text[]) {
int count = 0;
for (int i = 0; i < strlen(text); i++) {
char c = tolower(text[i]);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
count++;
}
}
return count;
}
int main() {
printf("%d\n", countVowels("Hello World")); // 3
printf("%d\n", countVowels("bcd")); // 0
printf("%d\n", countVowels("")); // 0
printf("%d\n", countVowels("APPLE")); // 2
return 0;
}
Result:
3
0
0
2
Try it Yourself »
Now all the tests give the expected result.
The Four Steps
Here are the four steps again, with what we did for the vowel problem. Notice the arrow back from step 4: when a test fails, you fix the code and test again, until all the tests pass:
for loop with an if inside."APPLE" gives 0, not 2: wrong!Tips for Solving Problems
- Solve one small example by hand first.
- Write the steps in plain words before you write code.
- Write a little code at a time, and run it often.
- Use
printf()to see what your variables contain (see Debugging). - Also test unusual values: empty input, only one value, uppercase letters, negative numbers, and very large numbers.
In the next chapters, you will learn more about these steps, starting with how to build the logic for a problem.