-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitials.c
51 lines (44 loc) · 1.03 KB
/
initials.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
/*
* initials.c
*
* Harvard CS50x3 - Problem Set 2 - Initials
*
* Prompts the user for their name and outputs their initials in uppercase
*
* Gábor Hargitai <[email protected]>
*
*/
#include <cs50.h>
#include <stdio.h>
#include <string.h>
// Capitalizes the selected character at the given index
// by analyzing its' ASCII value
void capitalize(char character[], int index)
{
if (character[index] >= 65 && character[index] <= 90)
{
printf("%c", character[index]);
}
else
{
printf("%c", (character[index]-32));
}
}
// The first character in the given name is automatically
// capitalized, after that it searches for a separating space
// character. Upon finding it, the next character after the
// space is capitalized.
int main(int argc, char *argv[])
{
string s = GetString();
capitalize(s, 0);
for (int i = 0; i < strlen(s); i++)
{
if (s[i] == ' ')
{
capitalize(s, i+1);
}
}
printf("\n");
return 0;
}