Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST TOOLS

C Tutorial

C HOME C Intro C Get Started C Syntax C Output C Comments C Variables C Data Types C Type Conversion C Constants C Operators C Booleans C If...Else C Switch C While Loop C For Loop C Break/Continue C Arrays C Strings C User Input C Memory Address C Pointers

C Functions

C Functions C Function Parameters C Pass By Reference C Scope C Function Declaration C Functions Challenge C Math Functions C Inline Functions C Recursion C Function Pointers

C Files

C Create Files C Write To Files C Read Files

C Structures

C Structures C Structs Challenge C Nested Structures C Arrays of Structs C Structs & Pointers C Unions C typedef C Struct Padding

C Enums

C Enums

C Memory

C Memory Management C Allocate Memory C Access Memory C Reallocate Memory C Deallocate Memory C Structs and Memory C Memory Example

C Errors

C Errors C Error Challenge C Debugging C NULL C Error Handling C Input Validation

C Problem Solving

C Problem Solving C Build the Logic C Edge Cases & Errors C Test Cases

C More

C Date C Random Numbers C Macros C Organize Code C Storage Classes C Bitwise Operators C Fixed-width Integers C Command-Line Args

C DSA

C DSA Intro C Searching C Sorting C Stacks C Queues C Linked Lists C Trees C Hash Tables C Big O

C Projects

C Projects

C Cert

C Certificate

C Reference

C Reference C Keywords C <stdio.h> C <stdlib.h> C <string.h> C <math.h> C <ctype.h> C <time.h>

C Examples

C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Practice Problems C Compiler C Syllabus C Study Plan C Interview Q&A

C Problem Solving


Share

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:

  1. Understand the problem
  2. Plan the solution
  3. Write the code
  4. 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:


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.




Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.