先从初级题入手:
Prob 1.求int型整数中1的个数
#include <stdlib.h>
#include <string.h>
#include "oj.h"
#include<stdio.h>
/*
功能:
输入:整型
输出:
返回:返回1的个数
*/
int GetCount(int iValue)
{
char bits[33];
memset(bits, 0, sizeof(char) * 33);
itoa(iValue, bits, 2);
// printf("\n%s\n", bits);
int cnt = 0;
for (int i = 0; i < 33; i++)
{
if (bits[i] == '1')
{
cnt++;
}
}
return cnt;
return 0;
}
Prob 3. 还是一道初级题 大写字母转换成小写字母,非字母过滤
#include <stdlib.h>
#include<stdio.h>
#include "oj.h"
#include <string.h>
/*
功能:将输入的字符串中英文大写字母改成对应小写字母,并且过滤掉非英文字母字符
输入:字符串
输出:结果字符串,保证输出地址有效。
返回:0表示成功,其它返回-1
*/
int ProcessString(char * strInput,char *strOutput)
{
if (NULL == strOutput) {
return -1;
}
int i = 0;
int j = 0;
while (strInput[i] != '\0')
{
if (strInput[i] >= 'A' && strInput[i] <= 'Z')
{
strOutput[j] = strInput[i]-'A'+'a';
// printf("%c\n", strOutput[j]);
j++;
}
else if (strInput[i] >= 'a' && strInput[i] <= 'z')
{
strOutput[j++] = strInput[i];
}
i++;
}
strOutput[j] = '\0';
return 0;
}
本文介绍两个C语言编程实例:一是计算整数中二进制表示的1的个数;二是将输入字符串中的大写字母转换为小写并过滤非字母字符。通过具体代码实现展示了基本的位操作及字符串处理技巧。

7931

被折叠的 条评论
为什么被折叠?



