Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
Split String
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
jf3g19
Split String
Commits
6e59028c
Commit
6e59028c
authored
4 years ago
by
jf3g19
Browse files
Options
Downloads
Patches
Plain Diff
Initial commit
parents
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
stringsplit.c
+61
-0
61 additions, 0 deletions
stringsplit.c
with
61 additions
and
0 deletions
stringsplit.c
0 → 100644
+
61
−
0
View file @
6e59028c
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
char
**
string_split
(
char
*
input
,
char
delim
)
{
char
**
split_strings
=
malloc
(
sizeof
(
char
*
));
char
*
charPtr
;
size_t
split_idx
=
0
;
size_t
num_allocated_strings
=
1
;
size_t
extend
=
0
;
for
(
charPtr
=
input
;
*
charPtr
!=
'\0'
;
++
charPtr
)
{
if
(
*
charPtr
==
delim
||
*
(
charPtr
+
1
)
==
'\0'
)
{
if
(
*
(
charPtr
+
1
)
==
'\0'
)
extend
=
1
;
//extend the range by one for the null byte at the end
char
*
string_element
=
calloc
(
1
,
sizeof
(
char
));
for
(
size_t
i
=
0
;
input
!=
charPtr
+
extend
;
++
input
,
++
i
)
{
if
(
string_element
[
i
]
==
'\0'
)
{
//allocate another char and add a null byte to the end
string_element
=
realloc
(
string_element
,
sizeof
(
char
)
*
(
strlen
(
string_element
)
+
1
));
string_element
[
i
+
1
]
=
'\0'
;
}
string_element
[
i
]
=
*
input
;
}
split_strings
[
split_idx
++
]
=
string_element
;
num_allocated_strings
++
;
//allocate another c-string if we're not at the end of the input
split_strings
=
realloc
(
split_strings
,
sizeof
(
char
*
)
*
num_allocated_strings
);
//skip over the delimiter
input
++
;
extend
=
0
;
}
}
free
(
charPtr
);
free
(
input
);
return
split_strings
;
}
int
main
()
{
char
*
str
=
"what day is it today?"
;
char
delim
=
' '
;
char
**
split
=
string_split
(
str
,
delim
);
printf
(
"split: %s
\n
"
,
split
[
0
]);
printf
(
"split: %s
\n
"
,
split
[
1
]);
printf
(
"split: %s
\n
"
,
split
[
2
]);
printf
(
"split: %s
\n
"
,
split
[
3
]);
printf
(
"split: %s
\n
"
,
split
[
4
]);
return
1
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment