#include <stdio.h> #include <conio.h>
unsigned int len(char *str) { unsigned int count; for (count = 0; *str != '\0'; str++, count++); return count; }
void copy(char *dest, char* str) { while (*str != '\0') { *dest = *str; *dest++; *str++; } *dest = '\0'; }
void concat(char *dest, char* str) { dest += len(dest); copy(dest, str); }
char equal(char *strA, char *strB) { char flag = 1; while (*strA != '\0') { if (*strB != *strA) { flag = 0; break; } strA++; strB++; } if (flag == 1 && *strA == '\0' && *strB != '\0') flag = 0; // Exception: st != str1 return flag; }
void show_equal(char* strA, char* strB) { if (equal(strA, strB)) printf("\t\"%s\" and \"%s\" are the same.\n", strA, strB); else printf("\t\"%s\" and \"%s\" are different.\n", strA, strB); }
void main() { char str1[20] = "Hello"; char str2[] = " World!"; concat(str1, str2);
printf("The length of str1 is %d.\n", len(str1)); printf("%s\n", str1);
printf("Now some strings will be compared:\n"); show_equal("str1", "str1"); show_equal("sdr1", "str1"); show_equal("Str1", "str1"); show_equal("str2", "str1"); show_equal("st", "str1"); show_equal("str1", "st"); show_equal("", ""); show_equal("not_empty", ""); show_equal("", "not_empty"); show_equal(str1, "Hello World!"); _getch(); }
|