将一个数组中的值按逆序重新存放。例如,原来顺序为 8,6,5,4,1。要求改为 1,4,5,6,8
#define _CRT_SECURE_NO_WARNINGS 1
#include
/*将一个数组中的值按逆序重新存放。例如,原来顺序为 8,6,5,4,1。要求改为 1,4,5,6,8*/
int main()
{
int arr[] = { 8,6,5,4,1 };
int sz = sizeof(arr) / sizeof(arr[0]);
int i = 0;
int temp = 0;
for (i = 0; i < sz / 2; i++)
{
temp = arr[i];
arr[i] = arr[sz - 1 - i];
arr[sz - 1 - i] = temp;
}
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
由图可得,推出公式:空格数量是2i
#define _CRT_SECURE_NO_WARNINGS 1
#include
int main()
{
int i = 0;
int j = 0;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 2 * i; j++)
{
printf(" ");
}
printf("* * * * *\n");
}
return 0;
}
有一篇短文,共有 3 行文字,每行有 80 个字符。想统计出其中英文大写字母,小写字母,数字、空格以及其他字符各有多少个。
#define _CRT_SECURE_NO_WARNINGS 1
/*有一篇短文,共有 3 行文字,每行有 80 个字符。
想统计出其中英文大写字母,小写字母,数字、空格以及其他字符各有多少个。*/
#include
int main()
{
char ch[3][80] = { '0' };
int i = 0;
int j = 0;
int upper = 0;
int lower = 0;
int number = 0;
int other = 0;
int space = 0;
for (i = 0; i < 3; i++)
{
gets(ch[i]);
}
for (i = 0; i < 3; i++)
{
for (j = 0; ch[i][j] != '\0'; j++)
{
if (ch[i][j] >= 'A' && ch[i][j] <= 'Z')
{
upper++;
}
else if (ch[i][j] >= 'a' && ch[i][j] <= 'z')
{
lower++;
}
else if (ch[i][j] >= '0' && ch[i][j] <= '9')
{
number++;
}
else if (ch[i][j] == ' ')
{
space++;
}
else
{
other++;
}
}
}
printf("upper:%d\n", upper);
printf("lower:%d\n", lower);
printf("num:%d\n", number);
printf("space:%d\n", space);
printf("other:%d\n", other);
return 0;
}
有一行电文,已按下面规律译成密码:
A->Z a->z
B->Y b->y
C->X c->x
即第 1个字母变成第 26 个字母,第 2个字母变成第 25 个字母,第i个字母变成第(26-i+1)个字母。非字母字符不变。假如已知道密码是 Umtorhs,要求编程序将密码译回原文,并输出密码和原文。
看大写字母部分,65-90就是大写字母的部分,加起来再减去密文,就能得到大写字母解码的部分,本题其实可以将65+90加起来到代码上,但是为了更清晰,采用了’A‘+’Z‘
对于小写字母,97-122就是小写字母部分,加起来再减去密文,就能得到小写字母解码部分
#define _CRT_SECURE_NO_WARNINGS 1
#include
int main()
{
char ch[100] = { '0' };
gets(ch);
int i = 0;
for (i = 0; ch[i] != '\0'; i++)
{
if (ch[i] >= 'A' && ch[i] <= 'Z')
{
ch[i] = 'A' + 'Z' - ch[i];
}
else if (ch[i] >= 'a' && ch[i] <= 'z')
{
ch[i] = 'a' + 'z' - ch[i];
}
else {
ch[i] = ch[i];
}
}
printf("%s", ch);
return 0;
}
编写一程序,将两个字符串连接起来,
首先知道这个函数的用法
声明
char *strcat(char *dest, const char *src)
参数
dest – 指向目标数组,该数组包含了一个 C 字符串,且足够容纳追加后的字符串。
src – 指向要追加的字符串,该字符串不会覆盖目标字符串。
本题的目标数组就是s1,追加的字符串就是s2
代码实现:
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
int main()
{
char s1[100] = { '0' };
char s2[50] = { '0' };
gets(s1);
gets(s2);
strcat(s1, s2);
printf("%s", s1);
return 0;
}
代码实现:
#define _CRT_SECURE_NO_WARNINGS 1
#include
int main()
{
char s1[100] = { '0' };
char s2[50] = { '0' };
int i = 0;
int j = 0;
gets(s1);
gets(s2);
while (s1[i] != '\0')
{
i++;
}
while (s2[j] != '\0')
{
s1[i] = s2[j];
i++;
j++;
}
s1[i] = '\0';
printf("%s", s1);
return 0;
}