C++ 如何在不使用字符串函数的情况下在char数组中查找字符串?(c+;+;)

C++ 如何在不使用字符串函数的情况下在char数组中查找字符串?(c+;+;),c++,C++,我有以下代码: char x[30]="computer"; char y[30]="put"; 我想知道“放”是否存在于“计算机”中,并打印它的位置。没有允许的字符串函数。 C++中,可以使用字符串和字符串::查找< /p> 对于char数组tho if (strstr(x, y) != NULL) { // contains } 库函数(c++风格) 不带库函数(c样式) 我想你不希望得到可怕的复杂性,所以这是你正在寻找的

我有以下代码:

char x[30]="computer";
char y[30]="put";

我想知道“放”是否存在于“计算机”中,并打印它的位置。没有允许的字符串函数。

C++中,可以使用字符串和字符串::查找< /p> 对于char数组tho

if (strstr(x, y) != NULL) {
    // contains
}
库函数(c++风格) 不带库函数(c样式)
我想你不希望得到可怕的复杂性,所以这是你正在寻找的
 std::string str ("computer"); std::string str2 ("put");

 std::size_t found = str.find(str2);
如果find=-1,则第二个字符串不在第一个字符串中


因为使用C++,可以使用STD::String:

#include <string>
#include <iostream>

const std::string stringToSearch("computer");
const std::string stringToFind("put");

if (stringToSearch.find(stringToFind) == std::string::npos) {
    std::cout << "not found\n";
} else {
    std::cout << "found \n";
}
#包括
#包括
常量std::字符串字符串搜索(“计算机”);
常量std::字符串stringToFind(“put”);
if(stringToSearch.find(stringToFind)=std::string::npos){

std::cout
std::strstr
std::search
可能很有用。您好,欢迎来到Stack Overflow。这个论坛通常是针对那些无法通过快速谷歌搜索来回答的问题。无论如何,您应该在问题中包含您的研究,您已经尝试了什么,结果是什么?坦率地说,这里不是解决家庭作业的地方。请使用std::string,而不是char数组!
#include <string>
#include <iostream>

const std::string stringToSearch("computer");
const std::string stringToFind("put");

if (stringToSearch.find(stringToFind) == std::string::npos) {
    std::cout << "not found\n";
} else {
    std::cout << "found \n";
}