-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtitlecase.c
69 lines (56 loc) · 1.13 KB
/
titlecase.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
60
61
62
63
64
65
66
67
68
69
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"cs50.h"
#define MAX 100
int isNotConjunction(string word)
{
string tab[]= {"the", "and", "of", "but"};
if (! (strncmp(tab[0], word, 3) && strncmp(tab[1], word, 3) && strncmp(tab[3], word, 3)))
if (!isalpha(word[3]))
return 0;
if (!strncmp(tab[2], word, 2))
if (!isalpha(word[2]))
return 0;
return 1;
}
void capitalizeLastWord(char * s)
{
int i;
for (i = strlen(s) - 1; i > 0; i--)
{
if (s[i-1] == ' ')
{
s[i] = toupper(s[i]);
break;
}
}
return ;
}
void capitalize(char * word)
{
word[0] = toupper(word[0]);
return;
}
int main(void)
{
char sep[]= " ";
string S = GetString();
char result[MAX];
result[0] = '\0';
string word = strtok(S, sep);
//Capitalize first word
capitalize(word);
strncat(result, word, strlen(word));
while(word = strtok(NULL, sep))
{
if (isNotConjunction(word))
capitalize(word);
strncat(result, " ", 1);
strncat(result, word, strlen(word));
}
strcat(result, "\0");
capitalizeLastWord(result);
printf("%s", result);
return 0;
}