#include <stdio.h>
#include <string.h>
#include <assert.h>
ElemNode *FindNfromEnd(ElemNode *head, unsigned int N);
int CountTimes(char *m_str, char *m_childstr);
void main(int argc, char *argv[])
{
printf("%d\n", CountTimes("qwertghyuiopghasdfghhjklghgh", "gh"));
return ;
}
/*******************************************************************************
*从长字符串中找出子字符串出现的次数?
*CountTimes("qwertghyuiopghasdfghhjklgh", "gh") out:4
******************************************************************************/
int CountTimes(char *m_str, char *m_childstr)
{
int count = 0;
for(int i = 0; i < strlen(m_str) - strlen(m_childstr) + 1; i++)
{
if(strncmp(m_str + i,m_childstr,strlen(m_childstr)) == 0)
count++;
}
return count;
}
/*******************************************************************************
*单向链表,找出倒数第N个元素?
*.......
******************************************************************************/
typedef struct Node
{
ElemType data;
struct Node *next;
}ElemNode;
ElemNode *FindNfromEnd(ElemNode *head, unsigned int N)
{
ElemNode *cursor = NULL;
ElemNode *shadow = NULL;
for(cursor = head, int i = 0; cursor->next != NULL; cursor = cursor->Next, i++)
{
if( i == N - 1)
shadow = head;
if(shadow != head)
shadow = shadow->Next;
}
return shadow;
}
/*******************************************************************************
*memcpy原型是void *memcpy(void *dest, const void *src, size_t count)
*写出my_memcpy要求针对32位系统优化.
******************************************************************************/
void *memcpy(void *dest, const void *src, size_t count)
{
assert((dest!=NULL)&&(src!=NULL));
char *tmp = (char *)dest;
char *p = (char *)src;
while(count--)
{
*tmp++ = *p++;
}
return dest;
}
void *my_memcpy(void *dest, const void *src, size_t count)
{
assert((dest!=NULL)&&(src!=NULL));
int sizenum = count/4;
int weishu = count%4;
int *tmp = (int *)dest;
int *p = (int *)src;
while(sizenum--)
{
*tmp++ = *p++;
}
while(weishu--)
{
*((char *)tmp++) = *((char *)p++);
}
return dest;
}