#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
int STRLEN(char *s)
{
int count = 0;
while (s[count] != '\0')
{
count++;
}
return count;
}
void STRCPY(char *s1, char *s2)
{
int length = STRLEN(s2);
for (int i = 0; i < length; i++)
{
s1[i] = s2[i];
}
s1[length] = '\0';
}
void STRLWR(char *s)
{
int length = STRLEN(s);
for (int i = 0; i < length; i++)
{
if (s[i] >= 'A' && s[i] <= 'Z')
{
s[i] += 32;
}
}
}
void STRUPR(char *s)
{
int length = STRLEN(s);
for (int i = 0; i < length; i++)
{
if (s[i] >= 'a' && s[i] <= 'z')
{
s[i] -= 32;
}
}
}
void HoanVi(char &a, char &b)
{
char Temp = a;
a = b;
b = Temp;
}
void STRREV(char *s)
{
int length = STRLEN(s);
for (int i = 0; i < length / 2; i++)
{
HoanVi(s[i], s[length - 1 - i]);
}
}
int STRCMP(char *s1, char *s2)
{
int length1 = STRLEN(s1);
int length2 = STRLEN(s2);
int LengthMin = length1 < length2 ? length1 : length2;
for (int i = 0; i < LengthMin; i++)
{
if(s1[i] < s2[i])
{
return -1;
}
else if(s1[i] > s2[i])
{
return 1;
}
}
if (length1 > length2)
{
return 1;
}
else if (length1 < length2)
{
return -1;
}
return 0;
}
int STRICMP(char *s1, char *s2)
{
/*STRLWR(s1);
STRLWR(s2);
return STRCMP(s1, s2);*/
int length1 = STRLEN(s1);
int length2 = STRLEN(s2);
int LengthMin = length1 < length2 ? length1 : length2;
for (int i = 0; i < LengthMin; i++)
{
char temp1 = s1[i];
char temp2 = s2[i];
// Quy hết tất cả về ký tự thường
if(temp1 >= 'A' && temp1 <= 'Z')
{
temp1 += 32;
}
if(temp2 >= 'A' && temp2 <= 'Z')
{
temp2 += 32;
}
if( temp1 < temp2)
{
return -1;
}
else if(temp1 > temp2)
{
return 1;
}
}
if (length1 > length2)
{
return 1;
}
else if (length1 < length2)
{
return -1;
}
return 0;
}
void STRCAT(char *s1, char *s2)
{
int length1 = STRLEN(s1);
int size1 = sizeof(s1);
int idx = 0;
int length2 = STRLEN(s2);
for (int i = 0; i < length2; i++)
{
s1[length1++] = s2[i];
if (length1 == size1 - 1)
{
break;
}
}
}
int STRSTR(char *s1, char *s2)
{
int length1 = STRLEN(s1);
int length2 = STRLEN(s2);
int Start;
bool Check;
for (int i = 0; i < length1; i++)
{
if (s1[i] == s2[0])
{
Start = i;
Check = true;
int Temp = Start;
for (int j = 1; j < length2; j++)
{
if (s1[++Temp] != s2[j])
{
Check = false;
break;
}
}
if(Check == true)
{
return Start;
}
}
}
return -1;
}
/*
strdup(s): Sao chép chuỗi s bỏ qua vùng
nhớ mới có độ lớn vừa đủ chứa
độ lớn sao chép: length + 1
*/
char * STRDUP(char *s)
{
int length = STRLEN(s);
char *p = (char *)malloc((length + 1) * sizeof(char));
for (int i = 0; i < length; i++)
{
p[i] = s[i];
}
p[length] = '\0';
return p;
}
/*
"1234"
Lần lượt lấy từng ký tự ra rồi đổi sang số
tương ứng
ta thấy kết quã mong muốn là 1234
1234 = 1 * 10^3 + 2 * 10^2 + 3 * 10^1 + 4 * 10^0;
*/
int ATOI(char *s)
{
int tong = 0;
int length = STRLEN(s);
int Temp = length;
// Xác định temp để biết phạm vi mình sẽ duyệt
for (int i = 0; i < Temp; i++)
{
if (s[i] < '0' || s[i] > '9') // Không phãi là số
{
Temp = i;
break;
}
}
int Temp1 = Temp;
for (int i = 0; i < Temp; i++)
{
tong += (s[i] - 48) * pow(10.0, --Temp1); // đổi ký tự sang số rồi nhân cho 10 mũ ...
}
return tong;
}
double ATOF(char *s)
{
char phannguyen[30];
int idx = 0;
char phanle[30];
int length = STRLEN(s);
bool Check = true;
for (int i = 0; i < length; i++)
{
if (Check == true)
{
phannguyen[idx++] = s[i];
}
else
{
phanle[idx++] = s[i];
}
if (s[i] == '.')
{
Check = false;
idx = 0;
}
}
return ATOI(phannguyen) + ATOI(phanle) / pow(10.0, idx);
}
void ITOA(int value, char *ketqua, int coso)
{
int idx = 0;
while(value != 0)
{
int so = value % coso;
if (so < 9)
{
ketqua[idx++] = so + 48;
}
else if(so == 10)
{
ketqua[idx++] = 'A';
}
else if(so == 11)
{
ketqua[idx++] = 'B';
}
else if(so == 12)
{
ketqua[idx++] = 'C';
}
else if(so == 13)
{
ketqua[idx++] = 'D';
}
else if(so == 14)
{
ketqua[idx++] = 'E';
}
else if(so == 15)
{
ketqua[idx++] = 'F';
}
value /= coso;
}
ketqua[idx] = '\0';
STRREV(ketqua);
}
// SUBSTR(s, x, y) // Cắt chuỗi con trong đoạn [x, y] của chuỗi s
char *SUBSTR(char *s, int x, int y)
{
char ketqua[100];
int length = STRLEN(s);
int idx = 0;
for (int i = x; i <= y; i++)
{
ketqua[idx++] = s[i];
}
ketqua[idx] = '\0';
return ketqua;
}
int TimViTriDauTien(char *s, char *find)
{
int length_s = STRLEN(s);
int length_find = STRLEN(find);
int Start;
bool Check;
for (int i = 0; i < length_s; i++)
{
if (s[i] == find[0])
{
Start = i;
Check = true;
int Temp = Start;
for (int j = 1; j < length_find; i++)
{
if (find[j] != s[++Temp])
{
Check = false;
break;
}
}
if (Check == true)
{
return Start;
}
}
}
return -1;
}
int main()
{
char s[] = "Vuong Tri Tai";
int length = STRLEN(s);
printf("\nlength = %d",length);
char s1[30];
STRCPY(s1,"Tai dep trai");
printf("\n %s",s1);
STRLWR(s);
printf("\nChu thuong: %s", s);
STRUPR(s);
printf("\nChu hoa: %s", s);
STRREV(s);
printf("\nChuoi dao nguoc: %s", s);
char s3[] = "abcd";
char s4[] = "Abed";
int kq = STRCMP(s3,s4);
printf("\nKq = %d",kq);
int kq2 = STRICMP(s3,s4);
printf("\nKq2 = %d",kq2);
char s5[20] = "abc";
char s6[7] = "defghi";
STRCAT(s5,s6);
printf("\ns5 = %s",s5);
char s7[] = "Nguyen Viet Nam Son";
char s8[] = "Nam";
int ViTri = STRSTR(s7,s8);
printf("\nVi tri = %d",ViTri);
char s9[] = "Son xau trai";
char *p = STRDUP(s9);
printf("\np = %s",p);
free(p);
char s10[] = "1275";
int x = ATOI(s10);
printf("\nx = %d", x);
char s11[] = "1275.54654";
double y = ATOF(s11);
printf("\ny = %lf", y);
int value = 250;
char ketqua[100];
int coso = 16;
ITOA(value,ketqua,coso);
printf("\nket qua = %s", ketqua);
char s12[] = "Nguyen Viet Nam Son";
char sub[100];
strcpy(sub,SUBSTR(s12,7,10));
printf("\nsub = %s", sub);
char s13[] = "Nguyen Viet Nam Son";
int ViTriDauTien = TimViTriDauTien(s13, "Viet");
printf("\nVitri = %d", ViTriDauTien);
getch();
return 0;
}