C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

文字'a'文字列を追加した後に文字列を検索するC++プログラムが非パリンドロームになります


小文字の英字を含む文字列Sがあるとします。 Sには文字「a」を1つだけ挿入する必要があります。それを挿入した後、Sを回文ではなくすることができる場合はその文字列を返し、そうでない場合は「不可能」を返します。

したがって、入力がS ="bpapb"のような場合、出力は "bpaapb"

になります。

ステップ

これを解決するには、次の手順に従います-

if concatenation of S and "a" is not palindrome, then:
   return S concatenation 'a'
otherwise when concatenation of "a" + S is not palindrome, then:
   return 'a' concatenation S
Otherwise
   return "Impossible"

理解を深めるために、次の実装を見てみましょう-

#include <bits/stdc++.h>
using namespace std;

bool p(const string& s) {
   for (int i = 0; i < s.size() / 2; i++)
   if (s[i] != s[s.size() - 1 - i])
      return false;
   return true;
}
string solve(string S) {
   if (!p(S + 'a'))
      return S + 'a';
   else if (!p('a' + S))
      return 'a' + S;
   else
      return "Impossible";
}
int main() {
   string S = "bpapb";
   cout << solve(S) << endl;
}

入力

"bpapb"

出力

bpapba

  1. 文字列の長さを見つけるC++プログラム

    文字列は、ヌル文字で終了する1次元の文字配列です。文字列の長さは、ヌル文字の前の文字列の文字数です。 たとえば。 char str[] = “The sky is blue”; Number of characters in the above string = 15 文字列の長さを見つけるプログラムは次のとおりです。 例 #include<iostream> using namespace std; int main() {    char str[] = "Apple";    int co

  2. 文字のASCII値を検索するC++プログラム

    ASCII(情報交換のためのアメリカ標準コード)テーブルには、0から127の範囲の値を持つ128文字があります。 異なる文字のASCII値のいくつかは次のとおりです- 文字 ASCII値 A 65 a 97 Z 90 z 122 $ 36 & 38 ? 63 文字のASCII値を見つけるプログラムは次のとおりです- 例 #include <iostream> using namespace std; void printASCII(char c) {    int i