用c++实现以下操作:编写一函数len,求一个字符串的长度,注意该长度不计空格。要求用字符指针实现。在主函数中输入字符串,调用该len函数后输出其长度。 int len(char *sp) { //实现sp所指串的长度,不计空格。 }
时间: 2024-05-06 13:20:47 浏览: 11
```c
#include <stdio.h>
int len(char *sp) {
int count = 0;
while (*sp != '\0') {
if (*sp != ' ') {
count++;
}
sp++;
}
return count;
}
int main() {
char str[100];
printf("请输入一个字符串:");
gets(str);
int length = len(str);
printf("该字符串的长度为:%d\n", length);
return 0;
}
```
相关问题
1、写一函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度,用指针处理。c++
以下是用指针处理字符串长度的函数:
```c++
#include <iostream>
using namespace std;
int strlen(const char* str) {
int len = 0;
while (*str) {
len++;
str++;
}
return len;
}
int main() {
char str[100];
cout << "请输入一个字符串:" << endl;
cin >> str;
int len = strlen(str);
cout << "该字符串的长度为:" << len << endl;
return 0;
}
```
首先定义了一个 `strlen` 函数,它接受一个 `const char*` 类型的指针参数,表示要计算长度的字符串。在函数中,用一个 `while` 循环遍历字符串,每遍历一个字符,长度加1,直到遍历完整个字符串。最后返回长度值。
在 `main` 函数中,先定义一个字符数组 `str`,用 `cin` 读入一个字符串。然后调用 `strlen` 函数计算字符串的长度,将结果存储到 `len` 变量中,并输出结果。
注意,这里输入字符串时使用了 `cin`,但如果输入的字符串包含空格,它只会读取第一个空格前面的部分作为一个字符串。如果要读取整个字符串,可以使用 `getline` 函数。同时,在输入时也要确保字符数组 `str` 能够存储下输入的字符串,否则可能会出现数组越界的错误。
c++ 编写一个函数,实现输入一个字符串,可以求出字符串当中的大写、小写、数字、空格以及其他的字符
题目要求定义一个一维字符数组string[100],输入一个字符串,含n个字符(n≤100),定义一个整形数组num[5],用于存放统计结果数据,编写函数count()统计字符串中大写字母、小写字母、空格、数字以及其他字符的个数,使用指针完成地址传递,主函数完成数组输入和统计结果输出。
具体实现可以参考以下代码:
```c++
#include <iostream>
#include <cstring>
using namespace std;
void count(char *str, int *num) {
int len = strlen(str);
for (int i = ; i < len; i++) {
if (str[i] >= 'A' && str[i] <= 'Z') {
num[]++; // 统计大写字母个数
} else if (str[i] >= 'a' && str[i] <= 'z') {
num[1]++; // 统计小写字母个数
} else if (str[i] == ' ') {
num[2]++; // 统计空格个数
} else if (str[i] >= '' && str[i] <= '9') {
num[3]++; // 统计数字个数
} else {
num[4]++; // 统计其他字符个数
}
}
}
int main() {
char str[100];
int num[5] = {}; // 初始化为
cout << "请输入一个字符串(长度不超过100):" << endl;
cin.getline(str, 100);
count(str, num);
cout << "大写字母个数:" << num[] << endl;
cout << "小写字母个数:" << num[1] << endl;
cout << "空格个数:" << num[2] << endl;
cout << "数字个数:" << num[3] << endl;
cout << "其他字符个数:" << num[4] << endl;
return ;
}
```
在主函数中,首先定义了一个字符数组str和一个整型数组num,其中num数组初始化为。然后通过cin.getline()函数输入一个字符串,再调用count()函数统计字符串中大写字母、小写字母、空格、数字以及其他字符的个数,最后输出统计结果。在count()函数中,使用指针完成地址传递,通过循环遍历字符串中的每个字符,根据字符的不同类型,分别统计到num数组的不同位置上。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)