C ++程序字符串类及其应用程序?

字符串是字符序列。在C ++编程语言中,可以通过两种方式定义字符串-

  • C样式字符串:将字符串视为字符数组。

  • C ++中的字符串类

  • 字符串类可以在库“字符串”中的C ++程序中使用。它将字符串作为字符数组存储在内存中,但向用户显示为字符串对象。在C ++中,有许多方法支持C ++字符串类,并有助于对象的适当功能并提高代码效率。

示例

在使用字符串的地方找到一些常见的字符串应用程序-

#include <bits/stdc++.h>
using namespace std;
bool charcheck(string str) {
   int l = str.length();
   for (int i = 0; i < l; i++) {
      if (str.at(i) < '0' || str.at(i) > '9')
         return false;
   }
   return true;
}
string replacedotWith20(string str) {
   string replaceby = "%20";
   int n = 0;
   while ((n = str.find(" ", n)) != string::npos ) {
      str.replace(n, 1, replaceby);
      n += replaceby.length();
   }
   return str;
}
int main() {
   string num = "3452i";
   if (charcheck(num))
      cout << "string contains only digit" << endl;
   else
      cout<<"string contains other characters too"<<endl;
   string url = "google com in";
   cout << replacedotWith20(url) << endl;
   return 0;
}

输出结果

字符串也包含其他字符。

google%20com%20in

上面的内容在使用Web时可能会有所帮助。