By GokiSoft.com|
15:47 08/12/2021|
Lập Trình C
[Video] Tìm kiếm chuỗi trong chuỗi - Lập trình C - C2110I
[Nâng cao] Tìm kiếm chuỗi trong chuỗi - Lập trình C
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
//khai bao 2 chuoi
char s1[50], s2[50];
//Nhap du lieu
printf("\nNhap s1 = ");
gets(s1);
printf("\nNhap s2 = ");
gets(s2);
//giai thuat tim kiem
int i, j, length1, length2;
length1 = strlen(s1);
length2 = strlen(s2);
bool isFind;
if(length1 >= length2) {
for(i=0;i<=length1-length2;i++) {
isFind = true;
for(j=0;j<length2;j++) {
if(s1[i+j] != s2[j]) {
isFind = false;
break;
}
}
if(isFind) {
printf("\nVi tri tim thay = %d", i);
}
}
} else {
//do dai s2 > doi dai s1 -> khong tim thay
printf("\nChuoi nhap vao ko hop le!!!");
}
return 0;
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)