文字列の部分文字列を返す関数を書きたいと思います。
f(what string, from which index, how many chars)
文字列クラスを使用して実行しましたが、char* を使用したいのですが、方法がわかりません。string* ではなく char* を使用するようにコードを修正していただけますか? C ++では紛らわしいです。
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
//index - starting at, n- how many chars
string* subString(string s, int index, int n){
string* newString = new string("");
for(int i = index; i < s.length() && i < n + index; i++)
*newString += s.at(i);
return newString;
}
int main()
{ string s1 = "Alice has a cat";
string* output = subString(s1, 2, 4);
cout<<(*output)<<endl;
system("pause");
return 0;
}