剑指offer 编程题(2):字符串替换

题目描述
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

class Solution {
public:
    void replaceSpace(char *str,int length) 
    {
        if(str == nullptr && length <= 0)
        {
            return ;
        }
        int count = 0;

        for( int i = 0; i<length; i++)
        {
            if(str[i] == ' ')
            {
                count++;
            }
        }
          int newlen = length + count*2;
          int m = newlen -1;
          int n = length -1;
          while(m > 0)
          {
              if(str[n] == ' ')
              {
                  str[m--] = '0';
                  str[m--] = '2';
                  str[m--] = '%';
                  n--;
              }
              else
              str[m--] = str[n--];
          }

    }
};
class Solution {
public:
    void replaceSpace(char *str,int length) 
    {
        if(str == nullptr && length == 0)
        {
            return ;
        }
        int count = 0;
        int i = 0;
        int old =0;
        while(str[i] != '\0')
        {

             if(str[i] == ' ')
            {
                count++;
            }
            old++;
            i++;
        }
          int newlen = old + count*2;
          int m = newlen;
          int n = old;
          while(m > 0)
          {
              if(str[n] == ' ')
              {
                  str[m--] = '0';
                  str[m--] = '2';
                  str[m--] = '%';
                  n--;
              }
              else
              str[m--] = str[n--];
          }

    }
};
class Solution {
public:
    void replaceSpace(char *str,int length) 
    {
        if(str == nullptr && length == 0)
        {
            return ;
        }
        int count = 0;
        int i = 0;
        int old =0;
        while(str[i] != '\0')
        {

             if(str[i] == ' ')
            {
                count++;
            }
            old++;
            i++;
        }
          int newlen = old + count*2;
          int m = newlen;
          int n = old;
         for(;n >= 0;n--)
         {
             if(str[n] != ' ')
             {
                 str[m--] = str[n];
             }
             else 
             {
                 str[m--] = '0';
                 str[m--] = '2';
                 str[m--] = '%';
             }
         }


   }
};

最佳方法利用string

class Solution {
public:
    void replaceSpace(char *str,int length) 
    {
        if(length <= 0)
        {
            return;
        }

        string s1 = str;
        string s2 = " ";
        int len =s2.length(); 
        int off = 0;
        off = s1.find(s2, 0);
        while (off != string:: npos)
        {
            s1.replace(off, len ,"%20");
            off = s1.find(s2, off + 1);
        }
        memset(str,0,length);
        const char *str11 = s1.c_str();
        strcpy(str,str11);

   }
};
class Replacement {
public:
    string replaceSpace(string iniString, int length) {
     while (iniString.find(" ") != -1){
        iniString = iniString.replace(iniString.find(' '), 1, "%20");
    }
     return iniString;
    }
};

1、如果要将string转换为char*,可以使用string提供的函数c_str() ,或是函数data(),data除了返回字符串内容外,不附加结束符’\0’,而c_str()返回一个以‘\0’结尾的字符数组。
2、const char *c_str();
c_str()函数返回一个指向正规C字符串的指针,内容与本string串相同.
这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式.
注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针
比如:最好不要这样:
char* c;
string s=”1234”;
c = s.c_str(); //
c最后指向的内容是垃圾,因为s对象被析构,其内容被处理
应该这样用:

char c[20];
string s="1234";
strcpy(c,s.c_str());

这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作
再举个例子
c_str() 以 char* 形式传回 string 内含字符串
如果一个函数要求char*参数,可以使用c_str()方法:

string s = "Hello World!";
printf("%s",s.c_str()); //输出 "Hello World!"

2、string 转换成 char *

如果要将string直接转换成const char *类型。string有2个函数可以运用。

一个是.c_str(),一个是data成员函数。

例子如下:

string s1 = "abcdeg";
const char *k = s1.c_str();
const char *t = s1.data();
printf("%s%s",k,t);
cout<<k<<t<<endl;

如上,都可以输出。内容是一样的。但是只能转换成const char*,如果去掉const编译不能通过。

那么,如果要转换成char*,可以用string的一个成员函数copy实现。

string s1 = "abcdefg";
char *data;
int len = s1.length();
data = (char *)malloc((len+1)*sizeof(char));
s1.copy(data,len,0);
printf("%s",data);
cout<<data;

3、char *转换成string

可以直接赋值。

string s;

char *p = "adghrtyh";

s = p;

不过这个是会出现问题的。

有一种情况我要说明一下。当我们定义了一个string类型之后,用printf(“%s”,s1);输出是会出问题的。这是因为“%s”要求后面的对象的首地址。但是string不是这样的一个类型。所以肯定出错。

用cout输出是没有问题的,若一定要printf输出。那么可以这样:
printf(“%s”,s1.c_str())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值