-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.c
More file actions
50 lines (47 loc) · 1.14 KB
/
Copy pathsplit.c
File metadata and controls
50 lines (47 loc) · 1.14 KB
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
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
/**
* main - a function that split string
*
* Return: always 0
*/
int main(void)
{
char inputstr[100];
char words[10][10];
int wordindex = 0;
int totalwords = 0;
int indexctr = 0;
printf("Enter a string: ");
fgets(inputstr, sizeof(inputstr), stdin);
/* loop through each character in the string */
for (indexctr = 0; (unsigned int)indexctr <= strlen(inputstr); indexctr++)
{
/* if current character equals space or null */
if (inputstr[indexctr] == ',' || inputstr[indexctr] == '\0')
{
/* append the null character to the current word */
words[totalwords][wordindex] = '\0';
totalwords++;
/* reset the index to zero for the next word */
wordindex = 0;
}
else
/* found a character of a word */
{
/* copy the character of a word */
words[totalwords][wordindex] = inputstr[indexctr];
/* increment the index for the word */
wordindex++;
}
}
printf("\nWords from the string are : \n");
/* loop through all the words that were copied */
for (indexctr = 0; indexctr < totalwords; indexctr++)
{
/* print each word */
printf("%s\n", words[indexctr]);
}
return (0);
}