-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcount_tokens.c
60 lines (54 loc) · 951 Bytes
/
count_tokens.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
#include "main.h"
/**
* count_words - count number of words in a string
* @str: string pointer
*
* Return: number of words,
* 0 on failure
*/
int count_words(char *str)
{
int len = 0, count = 0;
if (!str)
return (0);
for (; str[len]; len++)
{
if (str[len] == 32)
continue;
if (str[len + 1] == 32)
count++;
}
if (!len)
return (0);
if (str[len - 1] != 32)
count++;
if (count == 0)
return (0);
return (count);
}
/**
* count_tokens - count number of tokens in a string based on delmitier
* @str: string pointer
* @delim: string pointer
*
* Return: number of tokens,
* 0 on failure
*/
int count_tokens(char *str, char *delim)
{
int len = 0, count = 0;
if (!str)
return (0);
for (; str[len]; len++)
{
if (!str[len + 1] || _strchr(delim, str[len]))
continue;
if (_strchr(delim, str[len + 1]))
count++;
}
if (!len)
return (0);
if (!_strchr(delim, str[len - 1]))
count++;
return (count);
}