gpt4 book ai didi

c++ - 在 C++ 中查找 C 风格 char * 字符串的长度

转载 作者:行者123 更新时间:2023-11-28 01:56:45 25 4
gpt4 key购买 nike

<分区>

这个程序的重点是反转一个字符串并将其打印出来。这是一项学校作业,我才刚刚开始。我需要反转给我的字符串(整个 main 函数都是为赋值而给出的),我需要通过使用 2 个指针作为 cstring 的开头和 cstring 的结尾来完成。在代码中,我知道传递的 cstring 的开头只是“temp”,但我不知道如何找到 cstring 的结尾字母并为其分配指针。

主要功能是给我的:

#include <iostream>
#include "reverse.hpp"

using namespace std;
void reverse(char*);

int main()
{
// these are the test cases
char str1[] = "time";
char str2[] = "straw";
char str3[] = "deliver";
char str4[] = "star";
char str5[] = "knits";

cout << "The strings before reversing: " << endl;
cout << str1 << " " << str2 << " " << str3 << " " << str4 << " " << str5 << " " << endl;

reverse(str1);
reverse(str2);
reverse(str3);
reverse(str4);
reverse(str5);

cout << "The strings after reversing: " << endl;
cout << str1 << " " << str2 << " " << str3 << " " << str4 << " " << str5 << " " << endl;

return 0;

}

这里是反向函数:

#ifndef REVERSE_HPP
#define REVERSE_HPP
#include <string.h>

void reverse(char*temp)
{
// *temp would equal "t" in the first cstring
// (sizeof *temp - 1) would equal last character ???

}

#endif // REVERSE_HPP

这与其他发布的不同,因为我试图用指针获取长度

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com