笔试编程题

#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;
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值