用指针将s2中大写字母拼接到s1后面
时间: 2024-05-07 09:22:41 浏览: 15
可以使用指针遍历字符串s2,并将其中的大写字母添加到字符串s1的末尾,代码如下:
```c++
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char s1[100] = "hello, ";
char s2[100] = "WORLD!";
char *p = s2; // 定义指针p指向字符串s2的首地址
while (*p != '\0') { // 遍历字符串s2
if (*p >= 'A' && *p <= 'Z') { // 如果当前字符是大写字母
strcat(s1, p); // 将当前字符拼接到字符串s1的末尾
}
p++; // 指针p向后移动一位
}
cout << s1 << endl; // 输出拼接后的字符串s1
return 0;
}
```
输出结果为:
```
hello, WORLD
```
相关问题
不使用strcpy 函数用指针把s1复制到s2中
可以使用指针遍历s1和s2,逐个将s1中的字符赋值给s2,直到遇到'\0'为止。以下是示例代码:
```
void copyString(char *s1, char *s2) {
while (*s1 != '\0') {
*s2 = *s1;
s1++;
s2++;
}
*s2 = '\0';
}
```
调用方式为:
```
char s1[] = "Hello, world!";
char s2[20];
copyString(s1, s2);
printf("%s", s2);
```
输出结果为:
```
Hello, world!
```
c语言用指针处理,将字符串中大写字母换成小写字母
可以使用指针遍历字符串,对于每一个字符,判断它是否为大写字母,如果是,则将它转换成对应的小写字母。
以下是一个示例代码:
```c
#include <stdio.h>
void toLowerCase(char* str) {
char* p = str;
while (*p != '\0') {
if (*p >= 'A' && *p <= 'Z') {
*p += ('a' - 'A');
}
p++;
}
}
int main() {
char str[] = "Hello, World!";
printf("Original string: %s\n", str);
toLowerCase(str);
printf("After conversion: %s\n", str);
return 0;
}
```
输出:
```
Original string: Hello, World!
After conversion: hello, world!
```
注意,在函数 `toLowerCase` 中,我们使用指针 `p` 遍历字符串,判断每一个字符是否为大写字母,并将其转换为小写字母。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)