Skip to content

Commit 3be5acf

Browse files
committed
added: 6-cap_string.c
1 parent 89fd350 commit 3be5acf

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "main.h"
2+
3+
/**
4+
* checkSeparator - check allowed separators.
5+
*
6+
* @separator: char to be checked.
7+
*
8+
* Return: (1) if allowed separator, (0) otherwise.
9+
*/
10+
int checkSeparator(char separator)
11+
{
12+
char separators[] = {' ', '\t', '\n', ',', ';', '.', '!', '?', '"', '(', ')', '{', '}', '\0'};
13+
int size = sizeof(separators) / sizeof(*separators);
14+
int index;
15+
16+
for (index = 0; index < size; index++)
17+
{
18+
if (separator == separators[index])
19+
{
20+
return (1);
21+
}
22+
}
23+
return (0);
24+
}
25+
26+
/**
27+
* toUpper - Capitalize character.
28+
*
29+
* @c: character.
30+
*
31+
* Return: Uppercase character.
32+
*/
33+
char toUpper(char c)
34+
{
35+
char lower[] = "abcdefghijklmnopqrstuvwxyz";
36+
char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
37+
int size = sizeof(upper) / sizeof(*upper);
38+
int index;
39+
40+
for (index = 0; index < size; index++)
41+
{
42+
if(c == lower[index])
43+
{
44+
break;
45+
}
46+
}
47+
return (upper[index]);
48+
}
49+
/**
50+
* cap_string - Capitalize string.
51+
*
52+
* @str: string to be capitalized.
53+
*
54+
* Return: pointer to str.
55+
*/
56+
char *cap_string(char *str)
57+
{
58+
int index = 0;
59+
60+
while (str[index] != '\0')
61+
{
62+
if ((str[index] >= 97) && (str[index] <= 122))
63+
{
64+
if (checkSeparator(str[index - 1]))
65+
{
66+
str[index] = toUpper(str[index]);
67+
}
68+
}
69+
index++;
70+
}
71+
return (str);
72+
}

0x06-pointers_arrays_strings/6-main.c

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "main.h"
2+
#include <stdio.h>
3+
4+
/**
5+
* main - check the code
6+
*
7+
* Return: Always 0.
8+
*/
9+
int main(void)
10+
{
11+
char str[] = "Expect the best. Prepare for the worst. Capitalize on what comes.\nhello world! hello-world 0123456hello world\thello world.hello world\n";
12+
char *ptr;
13+
14+
ptr = cap_string(str);
15+
printf("%s", ptr);
16+
printf("%s", str);
17+
return (0);
18+
}

0x06-pointers_arrays_strings/main.h

+1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ char *_strcat(char *, char *);
66
char *_strncat(char *, char *, int);
77
char *_strncpy(char *, char *, int);
88
char *string_toupper(char *);
9+
char *cap_string(char *);
910

1011
#endif /* _MAIN_H_ */

0 commit comments

Comments
 (0)