输入一个长度为n(n≤10)的字符串(字符串中可能含有空格字符),把这个字符串奇数位上的字符删除后构成一个新的字符串,输出这个新的字符串
时间: 2023-04-02 21:02:56 浏览: 92
好的,我可以回答这个问题。可以使用循环遍历字符串,判断每个字符的下标是否为奇数,如果是则跳过该字符,否则将该字符加入新的字符串中。最后输出新的字符串即可。以下是示例代码:
```
#include <stdio.h>
#include <string.h>
int main() {
char str[11];
scanf("%s", str);
int len = strlen(str);
char new_str[6];
int j = ;
for (int i = ; i < len; i += 2) {
new_str[j++] = str[i];
}
new_str[j] = '\';
printf("%s\n", new_str);
return ;
}
```
相关问题
c++输入两个字符串,使用指针将两字符串连接,输入字符串中可能含有空格
可以使用指针将两个字符串连接起来,具体实现如下:
```c++
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str1[100], str2[100];
char *p1, *p2;
cout << "请输入第一个字符串:";
cin.getline(str1, 100); // 可以输入含有空格的字符串
cout << "请输入第二个字符串:";
cin.getline(str2, 100);
p1 = str1 + strlen(str1); // p1 指向 str1 的末尾
p2 = str2;
while (*p2 != '\0') {
*p1++ = *p2++; // 将 str2 的内容复制到 str1 的末尾
}
*p1 = '\0'; // 加上字符串结束符
cout << "连接后的字符串为:" << str1 << endl;
return 0;
}
```
上述代码中,先定义了两个字符数组 `str1` 和 `str2`,然后定义两个指针 `p1` 和 `p2`,分别指向两个字符串的首地址。接着使用 `strlen` 函数获取 `str1` 的长度,将 `p1` 指向 `str1` 的末尾。然后使用 `while` 循环,将 `str2` 中的内容复制到 `str1` 的末尾。最后加上字符串结束符,输出连接后的字符串。
输入一个长度为10的字符串,统计英文、数字、空格和其他字符的个数
您好,根据您的要求,我可以回答您的问题。对于一个长度为10的字符串,我们可以使用循环遍历每一个字符,然后使用 ASCII 码进行判断。具体的统计方法如下:
```c
#include <stdio.h>
int main() {
char str[11];
int i, alpha = , digit = , space = , other = ;
printf("请输入一个长度为10的字符串:");
scanf("%s", str);
for (i = ; i < 10; i++) {
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
alpha++;
} else if (str[i] >= '' && str[i] <= '9') {
digit++;
} else if (str[i] == ' ') {
space++;
} else {
other++;
}
}
printf("英文字母个数:%d\n", alpha);
printf("数字个数:%d\n", digit);
printf("空格个数:%d\n", space);
printf("其他字符个数:%d\n", other);
return ;
}
```
以上是统计英文、数字、空格和其他字符的个数的代码,希望能够帮到您。