c ++查找字符串_C ++字符串| 查找输出程序| 套装3

c ++查找字符串

Program 1:

程序1:

#include <iostream>
#include <string>
#include <string.h>
using namespace std;

int main()
{
    string str = "includehelp";
    char c_str[15];

    strcpy(c_str, str.c_str());

    cout << c_str;

    return 0;
}

Output:

输出:

includehelp

Explanation:

说明:

Here, we created the string object str, which is initialized with string "includehelp", And we created character array c_str with size 15.

在这里,我们创建了字符串对象str ,并使用字符串“ includehelp”对其进行了初始化,并创建了大小为15的字符数组c_str

strcpy(c_str,str.c_str());

Here, we used c_str() member function of the string class, which is used to convert a string object into a character array. Then we copy the resulted character array into the c_str()character array, and then we finally print the c_str that will print "includehelp" on the console screen.

在这里,我们使用了字符串类的c_str()成员函数,该函数用于将字符串对象转换为字符数组。 然后,我们将结果字符数组复制到c_str()字符数组中,然后最终打印将在控制台屏幕上打印“ includehelp”c_str

Program 2:

程式2:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "includehelp";

    for (int i = 0; i < str.length(); i++)
        cout << str.charAt(i) << " ";

    return 0;
}

Output:

输出:

main.cpp: In function ‘int main()’:
main.cpp:11:21: error: ‘std::string 
{aka class std::basic_string}’ has no member named ‘charAt’
         cout << str.charAt(i) << " ";
                     ^~~~~~

Explanation:

说明:

It will generate a syntax error. because charAt() is not a member function of class string. The at() member function of string class is used to get characters index-wise from the string.

它将生成语法错误。 因为charAt()不是类字符串的成员函数。 字符串类的at()成员函数用于从字符串按索引获取字符。

Program 3:

程式3:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "includehelp";

    char ch;

    for (int i = 0; i < str.length(); i++) {
        ch = str.at(i) - 32;
        cout << ch << " ";
    }

    return 0;
}

Output:

输出:

I N C L U D E H E L P

Explanation:

说明:

Here, we created a string str initialized with "includehelp" and we create a character variable ch. Here, we accessed character index-wise from string str, and we subtract each character by 32. Then each character will convert into uppercase.

在这里,我们创建了一个以“ includehelp”初始化的字符串str ,并创建了一个字符变量ch 。 在这里,我们从字符串str逐个索引访问字符,然后将每个字符减去32。然后每个字符将转换为大写。

Let's understand how it works? Here, the subtraction will work on ASCII values. The ASCII value of small 'i' is 105, after subtraction, the value will be 73, and we know that the 73 is the ASCII value of capital 'I', just like that all character will convert into uppercase.

让我们了解它是如何工作的? 在此,减法将对ASCII值起作用。 小字母“ i”的ASCII值为105,减后的值为73,我们知道73是大写字母“ I”的ASCII值,就像所有字符都将转换为大写字母一样。

Then finally "I N C L U D E H E L P" will be printed on the console screen.

最后, “ INCLUDEHELP”将被打印在控制台屏幕上。

翻译自: https://www.includehelp.com/cpp-tutorial/strings-find-output-programs-set-3.aspx

c ++查找字符串

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值