C言語のstrcat()関数とは何ですか?
Cライブラリ関数char* strcat(char * dest、const char * src) srcが指す文字列を追加します destが指す文字列の最後まで 。
文字の配列は文字列と呼ばれます。
宣言
以下は配列の宣言です-
char stringname [size];
例-charstring[50];長さ50文字の文字列
初期化
- 単一文字定数の使用-
char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’} - 文字列定数の使用-
char string[10] = "Hello":;
アクセス −「\0」に遭遇するまで文字列にアクセスするために使用される制御文字列「%s」があります。
strcat()関数
-
これは、2つの文字列を結合または連結するために使用されます。
-
宛先文字列の長さは、ソース文字列より長くする必要があります。
-
結果の連結文字列がソース文字列です。
構文
構文は次のとおりです-
strcat (Destination String, Source string);
サンプルプログラム
次のプログラムは、strcat()関数の使用法を示しています。
#include <string.h>
main(){
char a[50] = "Hello \n";
char b[20] = "Good Morning \n";
strcat (a,b);
printf("concatenated string = %s", a);
} 出力
上記のプログラムを実行すると、次の結果が得られます-
Concatenated string = Hello Good Morning
例
別の例を見てみましょう。
以下は、strcatライブラリ関数-
を使用してソース文字列を宛先文字列に連結するCプログラムです。#include<stdio.h>
#include<string.h>
void main(){
//Declaring source and destination strings//
char source[45],destination[50];
//Reading source string and destination string from user//
printf("Enter the source string : \n");
gets(source);
printf("Enter the destination string : \n");
gets(destination);
//Concatenate all the above results//
strcat(source,destination);
//Printing destination string//
printf("The modified destination string :");
puts(source);
} 出力
上記のプログラムを実行すると、次の結果が得られます-
Enter the source string :Tutorials Point Enter the destination string :C programming The modified destination string :Tutorials Point C programming
-
C言語のstrcmp()関数とは何ですか?
Cライブラリ関数intstrcmp(const char * str1、const char * str2) str1が指す文字列を比較します str2が指す文字列へ 。 文字の配列は文字列と呼ばれます。 宣言 以下は配列の宣言です- char stringname [size]; 例-charstring[50];長さ50文字の文字列 初期化 単一文字定数の使用- char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,&ls
-
C言語のstrcpy()関数とは何ですか?
Cライブラリ関数char* strcpy(char * dest、const char * src) srcが指す文字列をコピーします 宛先へ 。 文字の配列は文字列と呼ばれます。 宣言 以下は配列の宣言です char stringname [size]; 例-charstring[50];長さ50文字の文字列 初期化 単一文字定数の使用- char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}