C ++で1文字だけ変更して、文字列を回文文字列に変換します
このチュートリアルでは、1文字だけを変更して文字列を回文文字列に変換するプログラムについて説明します。
このために、文字列が提供されます。私たちのタスクは、1文字だけを変更することで、指定された文字列を回文に変換することです。
例
#include<bits/stdc++.h>
using namespace std;
//checking if conversion to palindrome
//is possible
bool if_palindrome(string str){
int n = str.length();
//counting number of characters
//to be changed
int count = 0;
for (int i = 0; i < n/2; ++i)
if (str[i] != str[n - i - 1])
++count;
return (count <= 1);
}
int main(){
string str = "abccaa";
if (if_palindrome(str))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
} 出力
Yes
-
C ++で単一の文字を文字列に変換するにはどうすればよいですか?
単一の文字を文字列に変換する方法はいくつかあります。次の例では、それらの一部を使用して文字を文字列に変換しています。 これは、C++言語で単一の文字を文字列に変換する例です。 例 #include <iostream> #include<string> #include<sstream> int main() { char c = 'm'; std::string s(1, c); std::cout << "Using string c
-
文字列内の文字の頻度を見つけるC++プログラム
文字列は、ヌル文字で終了する1次元の文字配列です。文字列内の文字の頻度は、文字列内で出現する回数です。例- String: Football is a sport The frequency of alphabet o in the above string is 3 特定のアルファベットの頻度を見つけるプログラムは次のとおりです。 例 #include <iostream> using namespace std; int main() { char str[100] = "this string contains many alphabet