[ create a new paste ] login | about

Link: http://codepad.org/Y658GDlC    [ raw code | output | fork ]

C, pasted on Nov 24:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>

int STRLEN(char s[])
{
	int dem = 0;
	while (s[dem] != '\0')
	{
		dem++;
	}
	return dem;
}

//trả về vị trí xuất hiện đầu tiên của chuỗi s1 trong chuỗi s và sẽ trả về giá trị NULL trong trường hợp không tìm thấy.
//int strstr(char *s, char *s1);

int STRSTR(char s1[], char s2[])
{
	int length1 = STRLEN(s1);
	int length2 = STRLEN(s2);
	int Start;
	bool Check;
	// duyệt chuỗi s1
	for(int i = 0; i < length1; i++)
	{
		if(s1[i] == s2[0])    // nếu bất kỳ phần tử nào trong s1 == phần tử đầu tiên của s2 
		{
			Start = i;         // bắt đầu đếm tại vị trí đó
			Check = true;
			int Temp = Start;
			for(int j = 1; j < length2; j++)
			{
				if(s1[++Temp] != s2[j])        // Kiểm tra coi có cái nào khác không ?
				{
					Check = false;
					break;
				}
			}
			if(Check == true)
			{
				return Start;
			}
		}
	}
	return -1;
}
int main()
{
	char s1[] = "Vuong Tri Tai";
	char s2[] = "Tai";

	int ViTri = STRSTR(s1, s2);
	printf("\nVi tri xuat hien cua chuoi |%s| trong chuoi |%s| la: %d", s2, s1, ViTri);

	getch();
	return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
Line 17: error: conio.h: No such file or directory
In function 'STRSTR':
Line 24: error: 'bool' undeclared (first use in this function)
Line 24: error: (Each undeclared identifier is reported only once
Line 24: error: for each function it appears in.)
Line 24: error: expected ';' before 'Check'
Line 26: error: 'for' loop initial declaration used outside C99 mode
Line 31: error: 'Check' undeclared (first use in this function)
Line 31: error: 'true' undeclared (first use in this function)
Line 33: error: 'for' loop initial declaration used outside C99 mode
Line 37: error: 'false' undeclared (first use in this function)


Create a new paste based on this one


Comments: