c语言实现12.编写程序,输入-一个字符串,要求将字符串中的空格替换为$符号,并记录替换的个数,最后显示替换后的字符串和替换个数。 例如:输入:aa bb cc dd 输出: aaSbbScc$dd 替换了3个字符
时间: 2023-05-21 13:03:39 浏览: 52
以下是C语言实现12的代码:
```
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int count = 0;
printf("请输入一个字符串:");
fgets(str, 100, stdin);
for (int i = 0; i < strlen(str); i++) {
if (str[i] == ' ') {
str[i] = '$';
count++;
}
}
printf("替换后的字符串为:%s\n", str);
printf("替换了%d个字符\n", count);
return 0;
}
```
关于lua closure factory的完整代码,我不是很清楚你具体指的是哪个问题,如果你能提供更多的信息,我可以尝试回答。
至于中文加密,这是一个比较广泛的话题,有很多种加密方式,比如替换、移位、异或等等。你需要具体说明你想了解哪种加密方式,我才能给出更具体的回答。
相关问题
用C语言编写程序,将一个字符串拷贝到另一个字符串中,拷贝过程将空格替换成“%20”。
好的,可以使用如下的代码来实现:
```c
#include <stdio.h>
#include <string.h>
void replaceSpace(char *str, int length) {
int spaceCount = 0;
int i;
for (i = 0; i < length; i++) {
if (str[i] == ' ') {
spaceCount++;
}
}
int newLength = length + spaceCount * 2;
int indexOld = length - 1;
int indexNew = newLength - 1;
while (indexOld >= 0 && indexOld < indexNew) {
if (str[indexOld] == ' ') {
str[indexNew--] = '0';
str[indexNew--] = '2';
str[indexNew--] = '%';
} else {
str[indexNew--] = str[indexOld];
}
indexOld--;
}
}
int main() {
char str[100] = "hello world";
replaceSpace(str, strlen(str));
printf("%s\n", str);
return 0;
}
```
这个程序中,我们定义了一个 `replaceSpace` 函数,用来将字符串中的空格替换成 `%20`。具体思路是,首先遍历一遍字符串,统计出空格的数量,然后计算出替换后的字符串长度。之后,从字符串的末尾开始遍历,遇到空格就替换为 `%20`,否则就直接复制到新的字符串中。最终得到的字符串即为替换后的结果。
.编写一个C语言程序,输入一个字符串,统计其中的字母、数字、空格和其他字符的个数并输出
好的,以下是您需要的C语言程序:
```c
#include<stdio.h>
int main(){
char str[100];
int i, alphabet, digit, space, other;
alphabet = digit = space = other = 0;
printf("请输入一个字符串:");
fgets(str, 100, stdin);
for(i=0; str[i]!='\0'; i++){
if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
alphabet++;
else if(str[i]>='0' && str[i]<='9')
digit++;
else if(str[i]==' ')
space++;
else
other++;
}
printf("字母个数:%d\n", alphabet);
printf("数字个数:%d\n", digit);
printf("空格个数:%d\n", space);
printf("其他字符个数:%d\n", other);
return 0;
}
```
程序说明:
1. 首先定义了一个字符数组 `str`,用于存储输入的字符串。
2. 定义了四个整型变量 `alphabet`、`digit`、`space` 和 `other`,用于统计字母、数字、空格和其他字符的个数。
3. 调用 `fgets()` 函数从标准输入流中读取字符串,并将其存储到 `str` 数组中。
4. 使用 `for` 循环遍历字符串中的每一个字符,判断其类型,并对相应的计数器进行加一操作。
5. 最后输出各种字符的个数。
注意:由于输入的字符串可能包含空格,因此不能使用 `scanf()` 函数直接读取字符串,需要使用 `fgets()` 函数读取。