Check if a string is concatenation of another given string | C ++ program to implement the above approach ; Function to check if a string is concatenation of another string ; Stores the length of str2 ; Stores the length of str1 ; If M is not multiple of N ; Traverse both the strings ; If str1 is not concatenation of str2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkConcat ( string str1 , string str2 ) { int N = str1 . length ( ) ; int M = str2 . length ( ) ; if ( N % M != 0 ) { return false ; } for ( int i = 0 ; i < N ; i ++ ) { if ( str1 [ i ] != str2 [ i % M ] ) { return false ; } } return true ; } int main ( ) { string str1 = " abcabcabc " ; string str2 = " abc " ; if ( checkConcat ( str1 , str2 ) ) { cout << " Yes " ; } else { cout << " No " ; } }