Find one extra character in a string | C ++ program to find extra character in one string ; Determine string with extra character . ; Add character codes of both the strings ; Add last character code of large string . ; Minus the character code of smaller string from the character code of large string . The result will be the extra character code . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char findExtraCharacter ( string s1 , string s2 ) { string smallStr ; string largeStr ; if ( s1 . size ( ) > s2 . size ( ) ) { smallStr = s2 ; largeStr = s1 ; } else { smallStr = s1 ; largeStr = s2 ; } int smallStrCodeTotal = 0 ; int largeStrCodeTotal = 0 ; int i = 0 ; for ( ; i < smallStr . size ( ) ; i ++ ) { smallStrCodeTotal += smallStr [ i ] ; largeStrCodeTotal += largeStr [ i ] ; } largeStrCodeTotal += largeStr [ i ] ; int intChar = largeStrCodeTotal - smallStrCodeTotal ; return ( char ) intChar ; } int main ( ) { string s1 = " abcd " ; string s2 = " cbdae " ; char extraChar = findExtraCharacter ( s1 , s2 ) ; cout << " Extra ▁ character : ▁ " << ( extraChar ) << endl ; return 0 ; }