1、将二进制转换为十进制(知识点:移位运算。eg:11010011变为211)
#include
#include
unsigned bin2int(const char* str)
{
int len = strlen(str); //获取字符串的长度
unsigned int sum = 0;
for (int i =0; i < len; i++)
{
//当字符值为1时,将1进行移位,再进行或运算
if (str[i]=='1')
{
unsigned int mask = 1u << (len - 1 - i);
sum |= mask;
}
}
return sum;
}
int main()
{
unsigned int ret = bin2int("11010011");
printf("the result is %d \n", ret);
return 0;
}
2、将输入的十六进制字符串转换为对应的整数(知识点:将16进制字符转换为十进制,再转换为整数。eg: “12AB3d”先变为整数的值:0x12AB3d ,即十进制的1223485,相当于1*16^5 + 2*16^4 +…..13*16^0)
#include
#include
//将十六进制的字符转换为整形
unsigned int hex_char_to_int(char ch)
{
if (ch>='0'&&ch<='9')
{
return ch - '0';
}
if (ch>='A'&&ch<='F')
{
return ch - 'A' + 10;
}
if (ch>='a'&&ch <= 'f')
{
return ch - 'a' + 10;
}
return 0;
}
//先将字符转换为十六进制数,再进行位数求和运算
unsigned hex_int(char* str)
{
int n = strlen(str);
int value = 0;
for (int i = 0; i < n; i++)
{
int num = hex_char_to_int(str[i]);
value = value * 16 + num;
}
return value;
}
int main()
{
unsigned int ret = hex_int("12AB3d");
printf("the result is %d \n", ret);
return 0;
}
3、写一个函数,判断一个数组是否“回文”: 即正着、倒着是一样的。
#include
#include
//判断是否为回文
int test(int* arr, int len)
{
for (int i = 0; i < len/2; i++)
{
if (arr[i] != arr[len-1-i])
{
return 0;
}
}
return 1;
}
int main()
{
int a[6] = { 1,2,3,3,2,1 };
int ret = test(a,6);
printf("the result is %d \n", ret);
return 0;
}
4、所有的小写变大写
#include
#include
//所有小写变大写
void to_uppercase(char* str)
{
int len = strlen(str);
for (int i = 0; i < len; i++)
{
if (str[i] >= 'a'&&str[i] <= 'z')
{
str[i] -= 32;
}
else
{
continue;
}
}
for (int i = 0; i < len; i++)
{
printf("%c", str[i]);
}
}
int main()
{
char buf[] = "It is a good book";
to_uppercase(buf);
return 0;
}
5、判断两个字符串是否相等。忽略大小写。(eg:例如,输入 “hello”和”hELlO”,认为它们是相等的。返回值:1表示相等。0表示不同)
#include
#include
//忽略大小写判断两字符串是否相等
int compare_ingore_case(const char* str1, const char* str2)
{
int len1 = strlen(str1);
int len2 = strlen(str2);
if (len1==len2)
{
for (int i = 0; i < len1; i++)
{
if (str1[i]==str2[i]||str1[i]==str2[i]-32||str1[i]==str2[i]+32)
{
return 1;
}
}
}
return 0;
}
int main()
{
char a[] = "hello";
char b[] = "HeLLo";
int ret = compare_ingore_case(a,b);
printf("the result is:%d \n", ret);
return 0;
}
6、读取用户输入的数,然后对它们从大到小排序后输出。(知识点:排序问题)
#include
#include
//定义全局变量
int count = 0;
int c[128];
//获取用户的输入
int get_num()
{
int a;
while (1)
{
printf("please input:");
int b = scanf_s("%d", &a);
if (b!=0)
{
c[count] = a;
count++;
}
else
break;
}
return count;
}
//从大到小排序
void uptodown(int* arr)
{
for (int i = 0; i < count-1; i++)
{
for (int j = i+1; j < count; j++)
{
if (arr[j]>=arr[i])
{
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
}
int main()
{
get_num();
uptodown(c);
printf("the total num is:%d \n", count);
for (int i = 0; i < count; i++)
{
printf("%d ", c[i]);
}
return 0;
}
7、求一个数组的平均值
#include
#include
//求一个数组的平均值
void get_avg(const char* arr, int n, char* avg)
{
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += arr[i];
}
*avg = sum / n;
}
int main()
{
char buf[5] = {12,23,34,12,23};
char c;
get_avg(buf, 5, &c);
printf("the avg is :%d \n", c);
return 0;
}
8、统计输入字符串的各个字母各出现了多少次。区分大小写。(知识点:字符串操作、统计、频率)
#include
#include
//统计字符串中个字符出现的次数
void stat(const char* str)
{
int times[52] = { 0 };
int len = strlen(str);
for (int i = 0; i < len; i++)
{
if (str[i]>='a'&&str[i]<='z')
{
int index = str[i] - 'a';//将字符转换数字下标:a-z对应0-25,A-Z对应26-52。
times[index] += 1;
}
if (str[i]>='A'&&str[i]<='Z')
{
int index = str[i] - 'A' + 26;
times[index] += 1;
}
}
for (int i = 0; i < 26; i++)
{
if (times[i]>0)
{
printf("%c:%d \n", i + 'a', times[i]);
}
}
for (int i = 26; i < 52; i++)
{
if (times[i]>0)
{
printf("%c:%d \n", i - 26 + 'A', times[i]);
}
}
}
int main()
{
char str[64] = "I am a good girl";
stat(str);
return 0;
}
9、将字符串里的字符排序后输出(只有a-z或A-Z),不区分大小写输出。(eg:输入Hello,输出eHllo。知识点:字符串,排序)
#include
#include
//统计字符串中个字符出现的次数
void paixu(char* str)
{
int len = strlen(str);
for (int i = 0; i < len-1; i++)
{
for (int j = i; j < len; j++)
{
//当第i个为小写时
if (str[i] >= 'a'&&str[i] <= 'z' )
{
if (str[j] >= 'a'&&str[j] <= 'z')
{
if (str[i] > str[j])
{
char temp = str[j];
str[j] = str[i];
str[i] = temp;
}
}
if (str[j] >='A'&&str[j]<='Z')
{
if (str[i] - 'a' > str[j] - 'A')
{
char temp = str[j];
str[j] = str[i];
str[i] = temp;
}
}
}
//当第i个为大写时
if (str[i] >= 'A'&&str[i] <= 'Z')
{
if (str[j] >= 'A'&&str[j] <= 'Z')
{
if (str[i] > str[j])
{
char temp = str[j];
str[j] = str[i];
str[i] = temp;
}
}
if (str[j] >= 'a'&&str[j] <= 'z')
{
if (str[i] - 'A' > str[j] - 'a')
{
char temp = str[j];
str[j] = str[i];
str[i] = temp;
}
}
}
}
}
//比较结束
for (int k = 0; k < len; k++)
{
printf("%c", str[k]);
}
}
int main()
{
char str[12] = "Hello";
paixu(str);
return 0;
}
10、将一个正整数转换成二进制字符串。(eg:将135转换成字符串得到 10000111 。知识点:位操作)
#include
#include
//将正整数转换为二进制
//value:输入整数,buf:输出字符串,num_of_bits:打印的位数
void to_binary(unsigned int value, char buf[], int num_of_bits)
{
for (int i = 0; i < num_of_bits; i++)
{
unsigned int mask = 1 << (num_of_bits - 1 - i);
//对计算机而言都是二进制操作,十进制和十六进制只不过是显示看的,可直接进行与操作
if (value & mask)
{
buf[i] = '1';
}
else
{
buf[i] = '0';
}
}
buf[num_of_bits] = 0;
}
int main()
{
char buf[128];
to_binary(135, buf, 12);
printf("%s \n", buf);
return 0;
}
代码基本上是自己写的,如有问题,可评论区留言讨论,如果喜欢,请动动手点个赞哦,谢谢。