#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string sentence = "some random sentence";
int i = 0; //runs through the bigger string
int j = 0; //runs through the smaller string
int k = 0; //variable to mark the position where the string starts being equal in order to delete it using substring
string remove = "random";
int a = sentence.size();
int b = remove.size();
while (i < a)
{
if (sentence[i] == remove[j])
{
if (b == j - 1)
{
cout << sentence.substr(0, k) << sentence.substr(i, (a - 1));
break;
}
else
{
i++;
j++;
}
}
else
{
i++;
j = 0;
k++;
}
}
return 1;
}
大きな文字列からrandomという単語を削除して印刷したいが、コードを実行しても何も返されない。何が欠けている? 私はすでに "cout"の下にブレークを置いてみましたが、動作しません。coutを使ってcoutを使ってwhileループを早く抜け出す
ありがとう:)
文章[i]を使用して単語にアクセスすることはできません。これは、i番目の場所に1文字のみを与えます。 – alDiablo
文章[i]は文字単位で比較し、物事が等しい最初の位置と、単語の終わり。これで、その単語の最初の文字までの部分文字列と、その単語の最後の文字の最後から始まる部分文字列を書きます。 –