第一题
#include <stdio.h>
#include <ctype.h>//用来把小写字母转换成大写字母的库函数
#include <string.h>
int main(void)
{
char str1[15];//存储的数组最大容量
int i, len;
printf ("Enter phone number: ");
gets(str1);//输入一串字符
len = strlen(str1);//测试字符长度
for (i = 0; i < len; i++)
{
str1[i] = toupper(str1[i]);//把小写字母转换成大写字母
switch (str1[i])
{
case 'A': case 'B': case 'C':
str1[i] = '2'; break;
case 'D': case 'E': case 'F':
str1[i] = '3'; break;
case 'G': case 'H': case 'I':
str1[i] = '4'; break;
case 'J': case 'K': case 'L':
str1[i] = '5'; break;
case 'M': case 'N': case 'O':
str1[i] = '6'; break;
case 'P': case 'Q': case 'R': case 'S':
str1[i] = '7'; break;
case 'T': case 'U': case 'V':
str1[i] = '8'; break;
case 'W': case 'X': case 'Y' : case 'Z':
str1[i] = '9'; break;
default : str1[i] = str1[i]; break;
}
}
str1[i] = '\0';
printf("In numeric form: %s", str1);
return 0;
}
第二题
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[20];
int i, j;
int len, index = 0;
printf ("Enter a first and last name: ");
gets (str);//输入一段字符
len = strlen (str);//测试字符串长度
printf ("You enerad the name: ");
for (i = len - 1; i >= 0; i--)//遍历逆序输出字符
{
if (str[i] == 32)//当输入的字符等于空格的时候就输出空格后面的字符
{
for (j = i + 1; j <= index + i; j++)
{
printf ("%c", str[j]);
}
}
else
index++;//记录索引
}
printf (", %c.", str[0]);//输出第一个字符
printf ("\n");
return 0;
}
第三题
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[80], str1[80];
int i, n, len;
printf ("Enter message to be encryted: ");
gets (str);
len = strlen (str);
printf ("Enter shift amount (1-25): ");
scanf ("%d", &n);
for (i = 0; i < len; i++)
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
str1[i] = ((str[i] - 'A') + n) % 26 + 'A';
}
else if (str[i] >= 'a' && str[i] <= 'z')
{
str1[i] = ((str[i] - 'a') + n) % 26 + 'a';
}
else
{
str1[i] = str[i];
}
}
printf ("Encrypted message: ");
for (i = 0; i < len; i++)
{
printf ("%c", str1[i]);
}
return 0;
}