6. 完成字符串数组以下功能: 1) 求字符串长度 2) 字符串复制 3) 字符串连接,例如:char c[10]=”wel”,d[10]=”come”;连接之后c[10]=”welcome” 4) 字符串大写变小写 5) 字符串小写变大写 6) 字符串比较大小
时间: 2024-05-14 11:17:22 浏览: 12
1) 求字符串长度的代码:
```c++
int length(char str[]) {
int len = 0;
for (int i = 0; str[i] != '\0'; i++) {
len++;
}
return len;
}
```
2) 字符串复制的代码:
```c++
void copy(char dest[], char src[]) {
int i;
for (i = 0; src[i] != '\0'; i++) {
dest[i] = src[i];
}
dest[i] = '\0';
}
```
3) 字符串连接的代码:
```c++
void concatenate(char dest[], char str1[], char str2[]) {
int i, j;
for (i = 0; str1[i] != '\0'; i++) {
dest[i] = str1[i];
}
for (j = 0; str2[j] != '\0'; j++) {
dest[i+j] = str2[j];
}
dest[i+j] = '\0';
}
```
4) 字符串大写变小写的代码:
```c++
void toLower(char str[]) {
int i;
for (i = 0; str[i] != '\0'; i++) {
if (str[i] >= 'A' && str[i] <= 'Z') {
str[i] += 32;
}
}
}
```
5) 字符串小写变大写的代码:
```c++
void toUpper(char str[]) {
int i;
for (i = 0; str[i] != '\0'; i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] -= 32;
}
}
}
```
6) 字符串比较大小的代码:
```c++
int compare(char str1[], char str2[]) {
int i;
for (i = 0; str1[i] != '\0' || str2[i] != '\0'; i++) {
if (str1[i] > str2[i]) {
return 1;
}
else if (str1[i] < str2[i]) {
return -1;
}
}
return 0;
}
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)