eg: mid = (begin + end) / 2
先将数据以升序的形式存储,再定义一个中间量,每次将所要检索的数据与中间量进行比较,根据比较结果重新确定新的中间量,每次筛选一半的数据,直至找到所要检索的数据或当检索完毕(begin > end)时停止循环。
eg1:
#include
int main(void)
{
int mid;
int i, n = 5;
int a[] = {1, 5, -8, 9, 3, 6, 10, -5, -9};
int len = sizeof(a) / sizeof(a[0]);
for(i = 0; i < len; ++i)
{
int j = i;
int t = a[j];
while(j > 0 && a[j - 1] > t)
{
a[j] = a[j - 1];
--j;
}
a[j] = t;
}
int begin = 0;
int end = len - 1;
while(begin <= end)
{
mid = (begin + end) / 2;
if(n < a[mid])
{
end = mid - 1;
}
else if(n > a[mid])
{
begin = mid + 1;
}
else
{
break;
}
}
if(begin <= end)
{
printf("found, in = %d\n", mid);
}
else
{
printf("no found\n");
}
return 0;
}
char c[10] = {'a','b','c'};
也可以写为: char c[10] = "abc";//char为数组类型,c为数组名(也指代数组首元素的地址), [10]为数组长度(长度为10),其元素个数为4个(字符串末尾有‘\0’,代表该字符串截止),有效元素个数为3.
字符型数组不能被整体赋值.
char c[] = "Hello"
因为,完整的字符数组中存储的字符串,末尾元素为‘\0’,因此,循环执行的条件为:c[i] != ‘\0’.
eg2:
//字符数组的遍历与打印
#include
int main(void)
{
char c[] = "Hello";
int i = 0;
while(c[i] != '\0')
{
putchar(c[i]);
++i;
}
printf("\n");
return 0;
}
字符数组的遍历和打印也可以直接调用puts函数实现
eg3:
#include
#include
int main(void)
{
char c[] = "Hello";
puts(c);
return 0;
}
scanf:
scanf("%s", 字符型数组名);
gets:
gets(字符型数组名);
fgets:
fgets((字符型数组名), (元素个数), stdin);
gets与fgets调用时需包含头文件: #include
.
strlen (数组名),用于求解字符串中的有效字符(即元素个数);
eg4:
#include
#include
int main(void)
{
char s[] = "Hello";
int len = strlen(s);
printf("%d\n", len);
return 0;
}
eg5:
#include
int main(void)
{
char s1[] = "Hello";
char s2[10];
int i = 0;
while(s1[i] != 0)
{
s2[i] = s1[i];
++i;
}
s2[i] = 0;
puts(s2);
return 0;
}
数组的复制功能也可以用strcpy实现;
strcpy(目标数组名,原数组名);
eg6:
#include
#include
int main(void)
{
char s1[] = "Hello";
char s2[10];
strcpy(s2, s1);
puts(s2);
return 0;
}
从首元素开始比较,判断是否相同,直至找到,不同元素,两元素相减,为正,则前者大,反之,后者大。
eg7:
#include
#include
int main(void)
{
char a[] = "Hello";
char b[] = "World";
char max[10];
int i = 0;
while(a[i] == b[i] && a[i] && b[i])
{
++i;
}
if(a[i] - b[i] > 0)
{
strcpy(max, a);
}
else
{
strcpy(max, b);
}
puts(max);
return 0;
}
调用strcmp函数实现比大小
一般形式:
strcmp(字符串1,字符串2); //比较大小
说明:字符串比较的规则与其他语言中的规则相同,即对两个字符串自左至右逐个字符相比(按 ASCII码值大小比较),直到出现不同的字符或遇到'\0'为止。如全部字符相同,则认为相等;若出现不相同的字符,则以第一个不相同的字符的比较结果为准。如果参加比较的两个字符串都由英文字母组成,则有一个简单的规律:在英文字典中位置在后面的为“大”。
eg8:
#include
#include
int main(void)
{
char a[] = "Hello";
char b[] = "World";
char c[] = "China";
char max[10];
strcpy(max, a);
if(strcmp(a, b) < 0)
{
strcpy(max, b);
}
else if(strcmp(max, c) < 0)
{
strcpy(max ,c);
}
puts(max);
return 0;
}
strcat(a, b)将数组b拼接到数组a;
eg9:
#include
int main(void)
{
char s1[20] = "Hello";
char s2[] = " world";
int i = 0;
while(s1[i])
{
++i;
}
int j = 0;
while(s2[j])
{
s1[i] = s2[j];
++i;
++j;
}
s1[i] = 0;
puts(s1);
return 0;
}
eg10:
#include
#include
int main(void)
{
char s1[100] = "Hello";
char s2[100] = "World!";
strcat(s1, s2);
puts(s1);
return 0;
}
与int 型数组,相同;
不过数组长度因为 strlen(s);
eg11:
#include
#include
int main(void)
{
char s[] = "Hello";
int i;
int len = strlen(s);
for(i = 0; i < len / 2; ++i)
{
char t = s[i];
s[i] = s[len - i -1];
s[len - i - 1] = t;
}
s[len] = 0;
puts(s);
return 0;
}