Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
master-embedded-systems
c-exercise-solutions public
Commits
9df26967
Commit
9df26967
authored
Nov 28, 2017
by
Dominik Widhalm
Browse files
Improved example solution for task 5.13
parent
7d81c93d
Changes
1
Hide whitespace changes
Inline
Side-by-side
ch_5/task_13/main.c
View file @
9df26967
...
...
@@ -16,10 +16,10 @@
/***** MACROS *****************************************************************/
// Maximum length of the input string
#
define STRING_MAX 80
#define STRING_MAX 80
/***** TYPEDEFS ***************************************************************/
typedef
enum
{
FOUND
,
NOFOUND
}
search_t
;
/***** STRUCTURES *************************************************************/
...
...
@@ -51,14 +51,14 @@ void string_parser (char* string, int* values) {
/* Numbers found index */
int
numbers
=
0
;
/* Flag if number is currently found */
in
t
num_found
=
0
;
search_
t
num_found
=
NOFOUND
;
/* Iterate over the input string */
while
((
*
(
string
+
index
)
!=
'\0'
)
&&
(
index
<
STRING_MAX
))
{
/* Check if current character is a digit */
if
((
*
(
string
+
index
)
>=
'0'
)
&&
(
*
(
string
+
index
)
<=
'9'
))
{
/* Check if previously digits were found */
if
(
num_found
==
1
)
{
if
(
num_found
==
FOUND
)
{
/* Shift left previously found number */
temp
*=
10
;
/* Add the current digit (ASCII to number) */
...
...
@@ -67,7 +67,7 @@ void string_parser (char* string, int* values) {
/* Set temp to the current digit (ASCII to number) */
temp
=
(
int
)
*
(
string
+
index
)
-
'0'
;
/* Set the number found flag */
num_found
=
1
;
num_found
=
FOUND
;
}
/* Check if next character is not a number */
...
...
@@ -77,7 +77,7 @@ void string_parser (char* string, int* values) {
/* Increment numbers found index */
numbers
++
;
/* Clear the number found flag */
num_found
=
0
;
num_found
=
NOFOUND
;
}
}
/* Increment index */
...
...
@@ -97,8 +97,8 @@ int main (void) {
/*** Local Variables ***/
/* Test string */
char
string
[
STRING_MAX
]
=
"183 jsb32 146 x7 hg316 h34g3k364n346 .09837"
;
/* Values found */
int
values
[
STRING_MAX
];
/* Values found
(maximum half the number of characters)
*/
int
values
[
STRING_MAX
/
2
];
/* Counter variable */
int
i
=
0
;
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment