C语言数组字符串的许多例题

目录:

1. 二分法查找值
2.查找多个相同的数的下标
3.计算字符串的长度
4.memset()
5.my_strcat()
6.my_strcmp()
7.计算一段话中单词的数目
8.my_memcpy()

二分法查找值

二分法查找:的条件是需要数组从小到大排序,而且要连续
11 22 33 44 55 66 77 88 99 100 //数组
0 1 2 3 4 5 6 7 8 9 //下标
left mid right
当输入的数value>mid时,left=mid+1;
当value<mid时,right=mid-1;
如果最后right<left错位了;说明在这个数组中没有查找的值

#define POINTERROR -1
#define NUMBERERROR -2
int find(int const *ar,int const value,int n)
{
	if (ar == NULL) return POINTERROR;
	int left = 0, mid = n / 2, right = n - 1;
	while (1)
	{	
		mid = (left + right) / 2;
		if (ar[mid] == value)
		{
			return mid;
		}
		else if (value > ar[mid])
		{
			left = mid + 1;
		}
		else if (value <ar[mid])
		{
			right = mid - 1;	
		}
		if (right < left)
		{
			return NUMBERERROR;
		}
		
	}
}
void main()
{
	while (1)
	{
		system("cls");
		int ar[] = { 12, 23, 34, 45, 56, 67, 78, 89, 90, 100 };
		int value;
		cin >> value;
		int pos;
		pos = find(ar, value, sizeof(ar) / sizeof(ar[0]));
		switch (pos)
		{
		case POINTERROR:cout << "指针无效" << endl; break;
		case NUMBERERROR:cout << "不存在该数" << endl; break;
		default:cout << "下标为"<<pos << endl;
		}
		char ch;
		cout << "是否继续:y/n" << endl;
		cin >> ch;
		if (ch == 'n')
		{
			break;
		}
	}
}

查找多个相同的数的下标

int find_val(int *ar, int val, int startpos, int size)
{
	for (int i = startpos; i < size; i++)
	{
		if (ar[i] == val)
		{
			return i;
		}
	}
	return -1;
}


void main()
{	
	int startpos=0;
	while ( (startpos = find_val(ar, 8, startpos, n)) !=-1)
	{
		printf("pos=%d	", startpos);
		startpos+=1;
	}
	if (startpos < 0)
	{
		printf("no value\n");
	}
}

计算字符串的长度

#define STRPNULL -1
int  my_strlen(const char *ch)
{
	const char *p=ch+1;
	if (ch == NULL) return STRPNULL;
	for ( ;*ch++!='\0';){}
	return (ch - p);
}
void main()
{
	char ch1[] = { "yhping" };
	const char *str = "yhping";
	int x = my_strlen(str);
	switch (x)
	{
	case STRPNULL:cout << "POINT ERROR" << endl;
	case 0:cout << "字符串为空" << endl;
	}
	cout << my_strlen(ch1) << endl;
	cout << my_strlen(str) << endl;
}

*进制计算公式total=total*进制+str-‘0’
例如

int total = 0;
	while ('\0' != *str)
	{
		if (!isdigit(*str))break;
		total = total * 10 + (*str - '0');
		++str;
	}

my_meset()

my_meset()函数是一个按字节赋值的函数,而不是按给类型赋值。
例如
int ar[10];
my_meset(ar,10,10);
的设想是将10个存储单元给赋值为10;
然而这样是无法实现的,他的实际作用是给10个字节赋值为10,而int ar[10];一共又40个字节所以,赋值10个字节是不符合int类型了储存规则的。所以打印ar[0]时不会出现10,而会出现40。
但是
char str[10];
my_meset(str,0,10);则可以完成对10个空间的赋值,应为char类型的数组一共只有10个字节。
另外memset()无法办到给数组赋值,只能作为初始化数组为全00000000或者
全ffffffff。把每个字节空间设置为0或者f

char str[10];
	int ar[10];
	int br[10];
	//my_meset(ar, 10.10);
	my_meset(str, 48, sizeof(str));

my_strcat()

#define POINTERROR -1
char *my_strcat( char *str1, const char *str2)
{
	if (str1 == NULL || str2 == NULL) return POINTERROR;
	char *p = str1;
	while (*str1!='\0')
	{
		str1++;
	}
	while (*str1++ = *str2++){}
	return p;
}
void main()
{
	char str1[20] = { "yhping" };
	char str2[20] = { "hellow" };
	my_strcat(str1, str2);
}

my_strcmp()

int my_strcmp(const char *str1, const char *str2)
{
	if (str1 == NULL || str2 == NULL)
	{
		exit(1);
	}
	int k = 0;
	while (((k = *str1 - *str2)) == 0 && str1++&&str2++);
	return k;
}
void main()
{
	char str1[20] = { "in" };
	char str2[20] = { "lo" };
	char str3[20] = { "yhping" };
	
	my_strcat(str1, str2);
	cout << my_strcmp(str1, str2);
}

计算一段话中单词的数目

需要掌握多状态设计,从而达到目的
如:start ,in_word ,out_word三个状态完成整个程序

//检测单词数目
struct words
{
	int number;
	int state;
};

words words_number(char *str);

words words_number(char *str)
{
	words word;
	word.number = 0;
	if (str == NULL)
	{
		word.state =pointerror;
		return word;
	}
	else
	{
		word.state = start;//正常单词
		while (*str != '\0')
		{
			switch (word.state)
			{
			case start:
				if (isalpha(*str))
				{
					word.state = in_word;
				}
				else
				{
					word.state = out_word;
				}
				break;
			case in_word:
				if (!isalpha(*str))//else keeping in_word
				{
					if (*str == ' ')
					{
						word.number += 1;
						word.state = start;
					}
					else word.state = out_word;
				}
				break;
			case out_word:
				if (isalpha(*str)&&*(str-1)==' ')
				{
					word.state = in_word;
				}//else keeping out_word
				break;
			}
			str++;
		}
	}
	return word;
}

my_memcpy()

void *my_memcpy(const void *p1, void *p2,  unsigned int n)//参数表的类型应该为无类型,n表示字节数这样即使在调用结构体或者数组是依旧可以使用,另外之所以在P1前加const是为了避免当复制对象是常量时,指针只能为常指针才能成功调用
{                                                         //另外之所以返回时是一个泛型指针,为的是个strcpy一样可以多次复制更加方便
#ifdef DEBUG
	if ((p1 == NULL) || (p2 == NULL))  //判空是十分重要的
	{
		return NULL;
	}
#endif
	char *p3 = (char*)p1;//之所以强转是为了达到每一个字节都复制的效果
	char *p4 = (char*)p2;//这里就是为了实现链式表达的作用从而达到多是使用
	while (n--)
	{
		*p4++ = *p3++;
	}
	return p2;
}
void main()
{
	node a = { 3, 'm' };
	node b;
	node c;
	my_memcpy(&a, &b, sizeof(node));
	my_memcpy(my_memcpy(&a, &b, sizeof(node)), &c, sizeof(node));
}
  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值