-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmax_numeric_value.c
56 lines (51 loc) · 1.11 KB
/
max_numeric_value.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
/* Program to extract maximum numeric value from a given alphanumeric string.
* For example:
* Enter a string: 100klh564abc365bg
* Maximum numeric value = 564
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void remove_newline(char str[]);
long long max_numeric_value(char str[]);
int main(void)
{
char str[26];
printf("Enter a string: ");
fgets(str, 26, stdin);
remove_newline(str);
long long maximum = max_numeric_value(str);
printf("Maximum numeric value = %lld\n", maximum);
return 0;
}
long long max_numeric_value(char str[])
{
long long num = 0;
long long max = 0;
int len_str = strlen(str);
for(int i = 0; i < len_str; i++)
{
if(str[i] >= '0' && str[i] <= '9')
{
num = num * 10 + (str[i] - '0');
if(num > max)
{
max = num;
}
}
else
{
num = 0;
}
}
return max;
}
void remove_newline(char* str)
{
size_t len_str = strlen(str);
len_str--;
if(*str && str[len_str] == '\n')
{
str[len_str] ='\0';
}
}