输入一个字符串,设计函数计算其有效长度,最终输出字符串长度及该字符串内容。不要使用系统提供的strlen函数。
时间: 2023-04-25 16:03:41 浏览: 115
可以设计一个函数,遍历字符串中的每一个字符,直到遇到字符串结束符'\0'为止,统计遍历过的字符数量,即为字符串的有效长度。具体实现可以参考以下代码:
```c
#include <stdio.h>
int getLength(char str[]) {
int len = 0;
while (str[len] != '\0') {
len++;
}
return len;
}
int main() {
char str[] = "Hello, world!";
int len = getLength(str);
printf("字符串长度为:%d\n", len);
printf("字符串内容为:%s\n", str);
return 0;
}
```
在上面的代码中,getLength函数接收一个字符串参数,使用while循环遍历字符串中的每一个字符,直到遇到字符串结束符'\0'为止,统计遍历过的字符数量,最终返回字符串的有效长度。在主函数中,我们调用getLength函数获取字符串的有效长度,并使用printf函数输出字符串长度和内容。
相关问题
设计一个字符串类string,包含输入字符串,返回字符串内容及长度等功能
好的,下面是一个简单的字符串类string的设计:
```c++
#include <iostream>
#include <cstring>
class string {
private:
char* str; // 字符串内容
int len; // 字符串长度
public:
// 构造函数
string(const char* s = "") {
len = std::strlen(s);
str = new char[len + 1];
std::strcpy(str, s);
}
// 析构函数
~string() {
delete[] str;
}
// 返回字符串内容
const char* c_str() const {
return str;
}
// 返回字符串长度
int length() const {
return len;
}
// 重载赋值运算符
string& operator=(const string& s) {
if (this != &s) {
delete[] str;
len = s.len;
str = new char[len + 1];
std::strcpy(str, s.str);
}
return *this;
}
// 重载加法运算符
string operator+(const string& s) const {
string temp;
temp.len = len + s.len;
temp.str = new char[temp.len + 1];
std::strcpy(temp.str, str);
std::strcat(temp.str, s.str);
return temp;
}
// 重载等于运算符
bool operator==(const string& s) const {
return (len == s.len) && (std::strcmp(str, s.str) == 0);
}
// 重载不等于运算符
bool operator!=(const string& s) const {
return !(*this == s);
}
// 重载小于运算符
bool operator<(const string& s) const {
return std::strcmp(str, s.str) < 0;
}
// 重载大于运算符
bool operator>(const string& s) const {
return std::strcmp(str, s.str) > 0;
}
// 重载小于等于运算符
bool operator<=(const string& s) const {
return std::strcmp(str, s.str) <= 0;
}
// 重载大于等于运算符
bool operator>=(const string& s) const {
return std::strcmp(str, s.str) >= 0;
}
// 重载下标运算符
char& operator[](int i) {
return str[i];
}
// 重载下标运算符(const 版本)
const char& operator[](int i) const {
return str[i];
}
// 友元函数,重载输出运算符
friend std::ostream& operator<<(std::ostream& os, const string& s) {
os << s.str;
return os;
}
// 友元函数,重载输入运算符
friend std::istream& operator>>(std::istream& is, string& s) {
char temp[1000];
is >> temp;
s = temp;
return is;
}
};
```
这样,我们就可以使用这个字符串类进行字符串的输入、输出和各种操作了。例如:
```c++
int main() {
string s1("hello");
string s2("world");
string s3 = s1 + s2;
std::cout << s3 << std::endl;
std::cout << s3.length() << std::endl;
return 0;
}
```
输出结果:
```
helloworld
10
```
题目描述 输入一个字符串,设计函数计算其有效长度,最终输出字符串长度及该字符串内容。不要使用系统提供的strlen函数。 输入要求 输入一个字符串,以换行符结束。字符串长度不超过100。 输出要求 输出有效长度及字符串内容。 输入样例 world 输出样例 5 world 模板 #include<stdio.h> @-@ //设计求字符串长度的函数 int main() { char str[101]; int len=0; gets(str); len=mylen(str); //调用函数求字符串长度 printf("%d ",len); printf("%s\n",str); return 0; }
好的,这是一个求字符串长度的问题,可以使用循环遍历字符串的每个字符,直到遇到字符串结束符'\0'为止,并记录字符数。以下是代码实现:
```
#include <stdio.h>
int mylen(char str[]) {
int len = 0;
while (str[len] != '\0') {
len++;
}
return len;
}
int main() {
char str[101];
int len = 0;
gets(str);
len = mylen(str);
printf("%d ", len);
printf("%s\n", str);
return 0;
}
```
输入样例:
```
world
```
输出样例:
```
5 world
```