字符指针功能以复制C 中的字符串
character pointer function to copy a string in c++
void pal_string_copy(char * source, char * destination){
for(int i = 0; i < *source.length(); i++)
{
if(isalpha(*source[i]))
{
destination += toupper(*source[i]) ;
}
}
我们必须与这些参数一起使用此功能,以在剥离空间并仅取字母的同时制作字符串的副本
成员参考基础类型'char *'不是结构或联合
调用函数时,是否可以将字符串作为参数传递,因为字符串是字符的向量?
看起来您将两个概念混合在一起-C字符串为char *
和C 字符串作为std::string
,然后尝试使用char *
,例如它们是std::string
(length((方法 = etc(,因此您的代码在C 中应该是这样的:
std::string pal_string_copy( const std::string &str )
{
std::string destination;
for(char c : str ) {
if(isalpha(c))
destination += toupper(c);
}
return destination;
}
或如果您必须使用C样式字符串,则不能使用方法,但是C函数strlen()
等。
char
没有成员称为 length
。代替char*
,您应该使用string
数据类型。和destination
字符串以reference
类型为单位,以便修改会影响呼叫功能,否则它将仅按值进行通过。
void pal_string_copy(std::string source, std::string &destination){
for(int i = 0; i < source.length(); i++) {
if(isalpha(source[i])) {
destination += toupper(source[i]) ;
}
}
}
int main() {
std :: string a = "hello", b ;
pal_string_copy(a,b);
std::cout <<" b = " <<b<<std::endl;
}
您正在尝试在char
值上调用不存在的length()
方法。这就是错误消息所抱怨的,因为char
不是具有方法的类型。您正在考虑使用std::string
类(您应该使用的是应该使用的代替char*
(。要获取char*
字符串的长度,请使用strlen()
函数。
另外,您正在错误地提出指针。不要一起使用*
和[]
操作员。在您的示例中,[]
足以从source
指针访问单个字符。但是,您还使用+=
运算符带有char*
指针,这也是错误的。同样,您正在考虑std::string
类。
如果要使用char*
指针,则需要使用更多类似的东西:
// prerequisite: the destination must point to a char[] buffer
// that is allocated to at least the same length as the
// char[] buffer that the source is pointing at...
void pal_string_copy(char * source, char * destination) {
int len = strlen(source);
for(int i = 0; i < len; ++i) {
if (isalpha(source[i])) {
*destination++ = (char) toupper(source[i]);
}
}
*destination = ' ';
}
否则,请改用std::string
,例如:
void pal_string_copy(const std::string & source, std::string & destination) {
destination.clear();
size_t len = source.length();
destination.reserve(len);
for(size_t i = 0; i < len; ++i) {
if (isalpha(source[i])) {
destination += (char) toupper(source[i]);
}
}
}
或:
std::string pal_string_copy(const std::string & source) {
std::string destination;
size_t len = source.length();
destination.reserve(len);
for(size_t i = 0, j = 0; i < len; ++i) {
if (isalpha(source[i])) {
destination += (char) toupper(source[i]);
}
}
return destination;
}
或使用STL算法:
std::string pal_string_copy(const std::string & source) {
std::string destination;
destination.reserve(source.length());
std::copy_if(source.begin(), source.end(), std::back_inserter(destination), [](unsigned char c) { return std::isalpha(c); });
std::transform(destination.begin(), destination.end(), [](unsigned char c) { return std::toupper(c); });
return destination;
}
您正在尝试将char *
视为std::string
类型对象。要使用char *
,您应该查找从C继承的功能来处理字符串(例如,std::strlen
以获取字符串的长度(。
此外,您在指针上的操作方式不正确。一种更好的写作方法是尝试使用迭代器和STL来最大程度地减少此类错误:
#include <string>
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <iterator>
int main() {
std::string orig = "test123", dest;
std::cout << "Original: " << orig << std::endl;
//Copies the string orig into dest but by only keeping the letters
std::copy_if(orig.begin(),orig.end(),std::back_inserter(dest),[](char ch) { return std::isalpha(ch); });
//Transforms each character in dest to upper case
std::for_each(dest.begin(),dest.end(),[](char &ch) { ch = std::toupper(ch); });
std::cout << "Transformed: " << dest << std::endl;
return 0;
}
编辑
这是通过字符串迭代的版本,但避免使用指针:
#include <string>
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <iterator>
int main() {
std::string orig = "test123", dest;
std::cout << "Original: " << orig << std::endl;
for( auto ch : orig ) {
if(std::isalpha(ch)) dest += std::toupper(ch);
}
std::cout << "Transformed: " << dest << std::endl;
return 0;
}
- 使用 strcpy_s 将字符串复制到字符*
- 在这种情况下,使用 string_view 是否会导致不必要的字符串复制?
- 无法使用 strcpy 将字符串复制到静态结构成员
- 我的 c 字符串复制函数正在损坏其他变量的堆栈
- 将 C 样式字符串复制到免费存储上分配的内存中
- strcpy_s将超过字符串长度的字符串复制到目标缓冲区(用 0xFE 填充)
- 将字符串复制到类中的常量字符
- 使用 copy_if 复制 std::字符串(复制到另一个字符串中)
- 将字符串复制到C 中的另一个字符串并打印其字符
- Linux/C++ 将字符串复制到剪贴板
- 将字符串复制到 char* 的现代C++方法
- 从一个字符串复制到另一个字符串并粘贴到新文件中
- 如何才能将字符串复制到带有 std::string 字段的结构中
- 如何将数字列表从字符串复制到C++向量
- 通过指针将字符串复制到结构
- 如何将包含多个空字符的字符串复制到字符串流
- 为什么我不能从字符串复制初始化字符串流?
- 如何做字符串::复制C++的反义词
- 即使在将新字符串复制到现有字符串之后,旧包含也可用(在某些索引中)
- c无法将字符串复制到文件中