#include <iostream>
#include <assert.h>
#include <string.h>
using namespace std;
char* Grial(char *s)
{
assert(s != NULL);
int len = strlen(s);
int count = 0;
char *p = s;
while (*p != ' ')
{
if (*p == ' ')count++;
p++;
}
int n = len + count * 2 + 1;
char *str = new char[n];
char *ret = str;
memset(str,' ',n);
strcpy(str,s);
p = str + n - 1;
char *q = str + len;
while (q < p)
{
if (*q == ' ')
{
*p-- = '0';
*p-- = '2';
*p = '%';
}
else
{
*p = *q;
}
q--;
p--;
}
return ret;
}
int main()
{
char s[] = "we are happy";
cout<<Grial(s)<<endl;
return 0;
}
#include <iostream>
#include <assert.h>
using namespace std;
bool Grial(const char *str1,const char *str2)
{
assert(str1!=NULL&&str2!=NULL);
const char *p = str1;
const char *q = str2 + strlen(str2) - 1;
if (strlen(str1) != strlen(str2))return 0;
while (*p != ' ')
{
if (*p != *q)return 0;
p++;
q--;
}
return 1;
}
int main()
{
char s1[] = "abcd";
char s2[] = "dcba";
cout << Grial(s1, s2) << endl;
return 0;
}*/
#include <iostream>
#include <assert.h>
using namespace std;
void Swap(char *p1,char *p2)
{
char temp;
while (p1 < p2)
{
temp = *p1;
*p1 = *p2;
*p2 = temp;
p1++;
p2--;
}
}
void Grial(char *s,int n)
{
assert(s!=NULL);
int len = strlen(s);
char *p = s + len - 1;
char *q = s;
char *midchar = NULL;
Swap(q,p);
midchar = p - n+1;
Swap(q,midchar-1);
Swap(midchar,p);
}
int main()
{
char s[] = "abcdef";
Grial(s, 3);
cout << s << endl;
return 0;
}*/
#include <iostream>
#include <assert.h>
#include <string.h>
using namespace std;
int my_atoi(char *p1,char *p2)
{
int count = 0;
while (p1 <= p2)
{
count = count * 10 + *p1 - '0';
p1++;
}
return count;
}
char * my_strstr(char *dist, char *src)
{
int n = strlen(src);
int num1 = my_atoi(src,src+n-1);
int flags = num1 % 13;
char *p = dist;
char *q;
char *m;
while (p <= dist + strlen(dist) - n)
{
m=p;
if (*p == *src)
{
if (my_atoi(p, p + n - 1) % 13 == flags)
{
q = src;
while (*q != ' ' && *p!=' ')
{
if (*q != *p)break;
q++;
p++;
}
if (*q == ' ')
return m;
}
}
m++;
p=m;
}
return NULL;
}
int main()
{
char s1[] = "123456789";
char s2[] = "789";
cout<<my_strstr(s1, s2)<<endl;
return 0;
}
#include <iostream>
#include <assert.h>
using namespace std;
bool IsNum(char ch)
{
if ((ch - '0') >= 0 && (ch - '0') <= 9)return true;
return false;
}
int GetNum(const char *s)
{
const char *p = s;
int count = 0;
while (*p != ' ')
{
if (*p == '.')return count;
if (count<0 )return 2147483647;
count = count * 10 + *p - '0';
p++;
}
}
int my_atoi(const char *s)
{
assert(s != NULL);
char ch = *s;
char flags;
if (IsNum(ch))
flags = IsNum(ch) + '0';
else
flags = ch;
const char *p = s + 1;
switch (flags)
{
case'+':
return GetNum(p);
break;
case '-':
if (GetNum(p) == 2147483647)
return 0 - GetNum(p) - 1;
return 0 - GetNum(p);
break;
case '1':
return GetNum(s);
break;
default:
exit(2);
}
return 0;
}
int main()
{
cout << my_atoi("-31321312321321") << endl;
return 0;
}
#include <iostream>
#include <assert.h>
using namespace std;
void* my_memmove(void *dist,void *src,int len)
{
assert(dist!=NULL&&src!=NULL);
char *pdist = (char *)dist;
void *ret = dist;
char *psrc = (char *)src;
if (pdist <= psrc || psrc + len <= pdist)
{
while (len--)
{
*pdist++ = *psrc++;
}
return ret;
}
else
{
pdist += len - 1;
psrc += len - 1;
while (len--)
{
*pdist-- = *psrc--;
}
return ret;
}
}
int main()
{
int a[] = { 2, 3, 4, 65, 1, 6, 7 };
my_memmove(a+1,a,8);
int *b = new int[7];
my_memmove(b,a,16);
int i = 0;
for (; i < 4; i++)
{
cout << b[i] << " ";
}
int i = 0;
for (; i < 7; i++)
{
cout << a[i] << " ";
}
cout << endl;
return 0;
}
#include <iostream>
#include <assert.h>
using namespace std;
void my_memcopy(void *a,const void *b, int len)
{
assert(a != NULL&&b != NULL);
char *p = (char *)a;
char *q = (char *)b;
while (len--)
{
*p++ = *q++;
}
}
int main()
{
int a[] = { 1, 2, 3, 4, 5 };
int *b = new int[5];
my_memcopy(b, a, sizeof(a));
int i = 0;
for (; i < 5; i++)
{
cout << b[i] << endl;
}
memcpy(b,a,100);
return 0;
}