#include
#include
using namespace std;
//模擬實現strcmp函數。
bool my_strcmp(const char *str1,const char *str2)
{
assert(str1!=NULL && str2!=NULL);
const char *p = str1;
const char *q = str2;
while (*p != '' && *q != '' && *p == *q)
{
p++;
q++;
}
return (*p - *q)!=0;
}
int main()
{
cout << my_strcmp(liwu, liu) << endl;
return 0;
}
#include
#include
using namespace std;
//庫函數memcopy的實現。
char *my_memcopy(char *dist, const char *src, int len)
{
assert(dist != NULL && src != NULL);
char *p = dist;
int n = len>(strlen(src)+1) ? strlen(src)+1 : len;
while (n-->0)
{
*dist++ = *src++;
}
return p;
}
int main()
{
char s1[20] = 1234;
my_memcopy(s1,liuhuiyan,2);
cout << s1 << endl;
}
#include
#include
using namespace std;
char* my_memove(char *dist,const char* src,int len)
{
assert(dist!=NULL && src!=NULL);
char *p = dist;
const char *q = src;
int n = len>(strlen(src) + 1) ? strlen(src) + 1 : len;
if (p <= q || q+n<=p)
{
while (n-- > 0)
{
*p++ = *q++;
}
}
else
{
p += n-1;
q += n-1;
while (n-- > 0)
{
*p-- = *q--;
}
}
return dist;
}
int main()
{
char s1[] = liu hui yan;
char s2[] = 123 456;
cout << my_memove(s1, s2, 6) << endl;
return 0;
}
#include
using namespace std;
//一個字符串“student a am i”,現要求將這個字符串修改為“i am a student”。
void exchange(char *p1,char *p2)
{
char temp;
while (p1 < p2)
{
temp = *p1;
*p1 = *p2;
*p2 = temp;
p1++;
p2--;
}
}
char* my_exchange(char *src)
{
char *p = src;
char *q = src;
while (*p != '')
{
while (*p != ' ' && *p!='')
{
p++;
}
exchange(q,p-1);
if (*p == '')break;
p++;
q = p;
}
exchange(src,p-1);
return src;
}
int main()
{
char s[] = student a am i;
cout << my_exchange(s) << endl;
}
//字符串打印數字。
#include
#include
using namespace std;
int my_int(const char *str)
{
assert(str!=NULL);
int count = 0;
while (*str != '')
{
count = (count * 10 + *str - '0');
str++;
}
return count;
}
int main()
{
char s[] = 12345;
cout << my_int(s) << endl;
return 0;
}
#include
using namespace std;
char str[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
//輸入數字1234,打印相應的字符串。
void Printf(int x)
{
if (x==0)return;
else
{
Printf(x / 10);
cout << str[x % 10] << endl;
}
}
int main()
{
Printf(1234);
return 0;
}