字符指针功能以复制C 中的字符串

character pointer function to copy a string in c++

本文关键字:字符串 复制 指针 功能 字符      更新时间:2023-10-16
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;
}