forked from thantrieu/LearnC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bai5.7.c
54 lines (47 loc) · 1.06 KB
/
bai5.7.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
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
int timViTri(char* str, int* length); // ham tim vi tri tu dai nhat trong xau
void hienThiTu(char *str, int pos, int length); // ham hien thi tu
bool khoangTrang(char c); // kiem tra ki tu co phai khoang trang hay khong
bool khoangTrang(char c) {
return c == ' ' || c == '\t' || c == '\n';
}
void hienThiTu(char *str, int pos, int length) {
printf("Tu dai nhat: \'");
int i;
for(i = pos; i < pos + length; i++) {
printf("%c", str[i]);
}
printf("\'\nDo dai: %d\nVi tri cua tu: %d", length, pos);
}
int timViTri(char* str, int* length) {
int len = 0;
int lenMax = 0;
int pos = 0;
int posCurrent = 0;
int size = strlen(str);
int i;
for(i = 0; i< size; i++) {
if(!khoangTrang(str[i])) { //hello ..
posCurrent = i - len;
len++;
} else {
if(len > lenMax) {
lenMax = len;
pos = posCurrent;
}
len = 0;
}
}
*length = lenMax;
return pos;
}
int main(){
char str[100];
fgets(str, 99, stdin);
int len = 0;
int pos = timViTri(str, &len);
hienThiTu(str, pos, len);
return 0;
}