-
Notifications
You must be signed in to change notification settings - Fork 121
/
count_spaces.c
59 lines (49 loc) · 1.65 KB
/
count_spaces.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*******************************************************************************
*
* Program: Count The Spaces In A String
*
* Description: Program to count the number of space characters in a string
* using C.
*
* YouTube Lesson: https://www.youtube.com/watch?v=LML3kmD1CtI
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <stdio.h>
#include <string.h>
int count_spaces(char *string);
int main(void)
{
// Test string with 4 spaces
char string[] = "Live life to the fullest";
// Call function to count spaces, store count into count
int count = count_spaces(string);
// Output the count
printf("Total Space Characters: %d\n", count)
return 0;
}
// Return the number of spaces in the string 'string'
int count_spaces(char *string)
{
// Find the length of the string (not including the null terminator) using
// strlen() from the string.h library, store the result into length
int length = strlen(string);
// Stores the count of spaces in the string, initialized to 0 because we
// have not counted anything yet
int count = 0;
// Loops through each index of the string from 0...length-1 with each loop
// iteration using the counter variable i. We use string[i] == ' ' to
// check if the character is a space and if it is we increment count to
// acknowledge we have found another space in the string.
for (int i = 0; i < length; i++)
{
if (string[i] == ' ')
{
count++;
}
}
// Once the loop is done we will have counted all the spaces in the string and
// we then return that count.
return count;
}