编写一个自己的大写转小写的函数 char A_t_a(char a);
并在主函数里调用测试验证;
要求:
1),只限于字母转换
2),如果输入的不是字母,增加判断处理使程序更加健壮。1.c
/**
* @file 1.c
* @brief convert upper to lower
* @author GuoPengyu
* @version 0.1
* @date 2011-09-09
*/
#include <stdio.h>
#include <string.h>
/**
* @brief upper to lower
*
* @param a upper
*
* @return result
*/
char A_to_a(char a)
{
if (a >= 'A' && a <= 'Z')
return a + 32;
else
return a;
}
int main(int argc, const char *argv[])
{
printf("%c\n", A_to_a('D'));
return 0;
}
实现一个函数,它能根据参数进行加法,减法,乘法,除法,取模运算(建议采用switch语句);2.c
/**
* @file 2.c
* @brief simple calculator
* @author GuoPengyu
* @version 0.1
* @date 2011-09-09
*/
#include <stdio.h>
/**
* @brief simple calculator with switch
*
* @param x integer
* @param y integer
* @param symbol like '+' '-' '*' '/'
*
* @return result
*/
int calc(int x, int y, char symbol)
{
switch(symbol)
{
case '+' : return x + y; break;
case '-' : return x - y; break;
case '*' : return x * y; break;
case '/' : return x / y; break;
case '%' : return x % y; break;
default : printf("symbol error!\n"); return -1; break;
}
}
int main(int argc, const char *argv[])
{
int x, y;
char symbol;
printf("Please input like this 3 + 4: ");
scanf("%d %c %d", &x, &symbol, &y);
printf("%d %c %d = %d\n", x, symbol, y, calc(x, y, symbol));
return 0;
}
统计一个自然数的二进制表示形式有多少个1;3.c
/**
* @file 3.c
* @brief 统计一个自然数的二进制表示形式有多少个1
* @author GuoPengyu
* @version 0.1
* @date 2011-09-09
*/
#include <stdio.h>
/**
* @brief 统计一个自然数的二进制表示形式有多少个1
*
* @param x the number
*
* @return result
*/
int calc(int x)
{
int count = 0;
while(x)
{
if (x & 0x01)//判断最后一位是否为1
count++;
x >>= 1; //same as x /= 2;
}
return count;
}
int main(int argc, const char *argv[])
{
int x;
printf("Input an integer: ");
scanf("%d", &x);
printf("%d one exists in %d", calc(x), x);
return 0;
}
统计一下某字符串中某指定字符出现的次数;4.c
/**
* @file 4.c
* @brief 统计某字符串中某指定字符出现的次数
* @author GuoPengyu
* @version 0.1
* @date 2011-09-09
*/
#include <stdio.h>
#include <string.h>
/**
* @brief 统计某字符串中某指定字符出现的次数
*
* @param s string
* @param c char
*
* @return number
*/
int parse(char *s, char c)
{
int count = 0;
while(*s)//字符串以‘\0’结束,其ASCII码为0,当到末尾时条件为假推出循环。
{
if (*s++ == c)
count++;
}
return count;
}
int main(int argc, const char *argv[])
{
char str[100];
char c;
printf("Please input a string ");
fgets(str, 100, stdin);//gets函数不推荐使用,不能指定大小,容易越界,scanf 不能接收空格。
str[strlen(str) - 1] = '\0';//fgets函数将会接受最后输入的回车符,去掉
printf("Please input the char to count: ");
scanf("%c", &c);
printf("%d %c exists in %s\n", parse(str, c), c, str);
return 0;
}
自己编写程序实现 void itoa(int x, char *p)(提示:将数字x转换为字符串并保存到p中);5.c
/**
* @file 5.c
* @brief 自己编写程序实现itoa()(提示:将数字x转换为字符串并保存到p中);
* @author Gaojinlong
* @version 0.1
* @date 2011-09-09
*/
#include <stdio.h>
#include <string.h>
/**
* @brief itoa
*
* @param x number to convert
* @param p store to p
*/
void myitoa(int x, char *p)
{
int i = 0, j = 0;
int tmp;
while(x)
{
p[i++] = x % 10 + '0';
x /= 10;
}
p[i] = '\0'; // add a '\0' to the end of string
for (i = 0, j = strlen(p) - 1; i <= j; i++, j--) //revert
{
tmp = p[i];
p[i] = p[j];
p[j] = tmp;
}
}
int main(int argc, const char *argv[])
{
int x;
char str[100];
printf("Please input an integer: ");
scanf("%d", &x);
myitoa(x, str);
printf("After convert, array str is %s\n", str);
return 0;
}