logo

C で 2 つの文字列を連結する

2 つの文字列 str1 と str2 が与えられた場合、私たちのタスクはこれら 2 つの文字列を連結することです。 C 言語で 2 つの文字列を連結するには、複数の方法があります。

  • strcat()関数を使用しない場合
    • 標準的な方法を使用する
    • 機能の使用
    • 再帰の使用
  • strcat()関数の使用

1. strcat() 関数を使用せずに 2 つの文字列を連結する

A. 標準的な方法を使用する

 Input: str1 = 'hello', str2 = 'world' Output: helloworld Input: str1 = 'Geeks', str2 = 'World' Output: GeeksWorld>

アプローチ: 「+」演算子の使用



C++






#include> #include> using> namespace> std;> int> main() {> >string str1 =>'Geeks'>;> >string str2 =>'ForGeeks'>;> >string result = str1 + str2;> >cout << result << endl;> >return> 0;> }>



>

>

出力

GeeksForGeeks>

アプローチ: 追加機能を使用します。

C++




#include> using> namespace> std;> int> main() {> >string str1 =>'hello'>;> >string str2 =>'world'>;> >cout<<>'The Resultant String Is :'>< cout< return 0; }>

>

>

出力

The Resultant String Is : helloworld>

複雑さの分析:

時間計算量:O(1)。

補助スペース: O(1)。

アプローチ:

  • 連結する 2 つの文字列を取得します
  • 連結された文字列を格納するための新しい文字列を宣言します
  • 最初の文字列を新しい文字列に挿入します
  • 新しい文字列に 2 番目の文字列を挿入します
  • 連結された文字列を出力する

上記のアプローチの実装を以下に示します。

C




// C Program to concatenate two> // strings without using strcat> #include> > int> main()> {> > >// Get the two Strings to be concatenated> >char> str1[100] =>'Geeks'>, str2[100] =>'World'>;> > >// Declare a new Strings> >// to store the concatenated String> >char> str3[100];> > >int> i = 0, j = 0;> > >printf>(>' First string: %s'>, str1);> >printf>(>' Second string: %s'>, str2);> > >// Insert the first string> >// in the new string> >while> (str1[i] !=>' '>) {> >str3[j] = str1[i];> >i++;> >j++;> >}> > >// Insert the second string> >// in the new string> >i = 0;> >while> (str2[i] !=>' '>) {> >str3[j] = str2[i];> >i++;> >j++;> >}> >str3[j] =>' '>;> > >// Print the concatenated string> >printf>(>' Concatenated string: %s'>, str3);> > >return> 0;> }>

>

>

C++




// C++ Program to concatenate two> // strings without using strcat> #include> using> namespace> std;> > int> main()> {> > >// Get the two Strings to be concatenated> >char> str1[100] =>'Geeks'>, str2[100] =>'World'>;> > >// Declare a new Strings> >// to store the concatenated String> >char> str3[100];> > >int> i = 0, j = 0;> > >cout <<>' First string: '><< str1;> >cout <<>' Second string: '><< str2;> > >// Insert the first string> >// in the new string> >while> (str1[i] !=>' '>) {> >str3[j] = str1[i];> >i++;> >j++;> >}> > >// Insert the second string> >// in the new string> >i = 0;> >while> (str2[i] !=>' '>) {> >str3[j] = str2[i];> >i++;> >j++;> >}> >str3[j] =>' '>;> > >// Print the concatenated string> >cout <<>' Concatenated string: '><< str3;> > >return> 0;> }> // this code is contributed by shivanisingh>

>

>

出力

First string: Geeks Second string: World Concatenated string: GeeksWorld>

時間計算量: O(m+n)
補助スペース: ○(1)

B. 関数の使用

アプローチ:

  • main 関数は concatenate_string() 関数を呼び出して 2 つの文字列を連結します。
  • この関数は、strlen を使用して文字列 s の長さを取得します。
  • 次に、文字列 s1 の文字を s[i+j] に追加します。このステップは、s1 で使用可能な文字がなくなるまで繰り返されます。文字列 s1 の文字を s の末尾から s に追加します。
  • for ループの後、文字列 s を連結します。
  • 最後に main 関数は連結された文字列を出力します。

C




// C program to concatenating two> // strings using function> #include> #include> void> concatenate_string(>char>* s,>char>* s1)> {> >int> i;> >int> j =>strlen>(s);> >for> (i = 0; s1[i] !=>' '>; i++) {> >s[i + j] = s1[i];> >}> >s[i + j] =>' '>;> >return>;> }> int> main()> {> >char> s[5000], s1[5000];> >printf>(>'Enter the first string: '>);> >gets>(s);> >printf>(>'Enter the second string: '>);> >gets>(s1);> >// function concatenate_string> >// called and s and s1 are> >// passed> >concatenate_string(s, s1);> >printf>(>'Concatenated String is: '%s' '>, s);> >return> 0;> }>

>

>

出力:

Enter the first string: Geeks Enter the second string: forGeeks Concatenated String is: 'techcodeview.com'>

時間計算量: O(n+m) 。ここで、n は文字列 1 のサイズ、m は文字列 2 のサイズです。
補助スペース: ○(1)

C. 再帰の使用

アプローチ:

  • 関数 concatenate_string() は文字列 s と s1 を取得します。
  • s1 に要素が存在しない場合は、s1 にヌル ( ) 文字を割り当てます。
  • それ以外の場合、要素が存在する場合は、文字列 s の末尾に文字列 s1 の要素を追加し、i の値を 1 ずつ増やします。
  • concatenate_string 関数は、変更された文字列 s、s1 を引数として渡すことによってそれ自体を呼び出します。この関数は、s1 で使用可能な要素がなくなるまで、それ自体を再帰的に呼び出します。

C




// C program to concatenate two> // strings with the help of> // recursion> #include> #include> void> concatenate_string(>char>* s,>char>* s1)> {> >static> int> i = 0;> >static> int> j =>strlen>(s);> >if> (!s1[i]) {> >s1[i] =>' '>;> >}> >else> {> >s[i + j] = s1[i];> >i++;> >concatenate_string(s, s1);> >}> }> int> main()> {> >char> s[5] =>'Geeks'>, s1[8] = 'forGeeks;> >// function concatenate_string> >// called and s1 and s2 are> >// passed> >concatenate_string(s, s1);> >printf>(>' Concatenated String is: '%s' '>, s);> >return> 0;> }>

>

>

出力:

Enter the first string: Geeks Enter the second string: forGeeks Concatenated String is: 'techcodeview.com'>

時間計算量: O(n+m) 。ここで、n は文字列 1 のサイズ、m は文字列 2 のサイズです。
補助スペース: ○(1)

2. strcat()関数の使用

C の strcat() 関数は、ソース文字列のコピーを文字列の末尾に Null 文字を付けてコピー先に追加します。これは、C の string.h ヘッダー ファイルの下にあります。

C




// C program to concatenate two> // strings using strcat function> #include> #include> int> main()> {> >char> s[] =>'Geeks'>;> >char> s1[] =>'forGeeks'>;> >// concatenating the string> >strcat>(s, s1);> >printf>(>'Final string is: %s '>, s);> >return> 0;> }>

>

>

C++


ネットワークアーキテクチャ



#include> #include> using> namespace> std;> int> main()> {> >char> s[] =>'Geeks'>;> >char> s1[] =>'forGeeks'>;> >// concatenating the string> >strcat>(s, s1);> >cout <<>'Final string is: '> << s;> >return> 0;> }> // This code is contributed by Akshay> // Tripathi(akshaytripathi630)>

>

>

出力

Final string is: techcodeview.com>

時間計算量: O(n+m) 。ここで、n は文字列 1 のサイズ、m は文字列 2 のサイズです。
補助スペース: ○(1)