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

変更して連結した後、指定された3つの文字列を出力します


3つの文字列を入力し、各文字列をユーザーが入力した文字に置き換えて、編集した文字列を表示します。その後、編集した文字列を連結して表示します。

Input:
   string 1 : tutorials replacement character for string 1 : x
   String 2 : points replacement character for string 2 : y
   String 3: best replacement character for string 3 : z
Output :
   string 1: xxxxxxxxx
   String 2 :yyyyyy
   String 3 : zzzz
   After concatenation : xxxxxxxxxyyyyyyzzzz

アルゴリズム

START
Step 1-> declare three array of characters str1, str2 and str3 with variables as ch1, ch2 and ch3 and int
variable as i
Step 2-> input strings str1, str2, and str3 with replacements characters as ch1, ch2, ch3
Step 3-> Loop For from i to 0 and i < strlen(str1) and i++
   Set str1[i]=ch1
   End For Loop
Step 4-> Loop For from i to 0 and i < strlen(str2) and i++
   Set str2[i]=ch2
   End For Loop
Step 5-> Loop For from i to 0 and i < strlen(str3) and i++
   Set str3[i]=ch3
   End For Loop
Step 6-> print edited string str1, str2 and str3
Step 7 -> concatenate str1 with str2 and store in str1
Step 8 -> concatenate str1 with str3 and store in str1
Step 9 -> print concatenated string
STOP

#include<stdio.h>
#include<string.h>
int main() {
   char str1[1000],str2[1000],str3[1000],ch1,ch2,ch3;
   int i;
   printf("\nenter a string : ");
   scanf("%s\n%s\n%s", &str1, &str2, &str3);
   fflush(stdin);
   printf("\nenter the character you want to replace your first, second and third string with ? ");
   scanf("%c %c %c", &ch1, &ch2, &ch3);
   for (i = 0; i < strlen(str1); i++) //replacing string 1 with ch1
      str1[i]=ch1;
   for (i = 0; i < strlen(str2); i++) //replacing string 2 with ch2
      str2[i]=ch2;
   for (i = 0; i < strlen(str3); i++) //replacing string 3 with ch3
      str3[i]=ch3;
   printf("string 1 after replacement with %c : %s\n",ch1,str1);
   printf("string 2 after replacement with %c : %s\n",ch2,str2);
   printf("string 3 after replacement with %c : %s\n",ch3,str3);
   strcat(str1,str2); //conactenate string 1 and string 2
   strcat(str1,str3); //conactenate string 1 and string 2
   printf("string after concatenation : %s",str1);
   return 0;
}

出力

上記のプログラムを実行すると、次の出力が生成されます

enter a string : tutorials
point
best
enter the character you want to replace your first, second and third string with ?
x
y
z
string 1 after replacement with x : xxxxxxxxx
string 2 after replacement with y : yyyyy
string 3 after replacement with z : zzzz
string after concatenation : xxxxxxxxxyyyyyzzzz

  1. 文字列とは何ですか? C言語で文字列を宣言して初期化します

    文字の配列(または)文字の集合は文字列と呼ばれます。 宣言 以下の宣言を参照してください- char stringname [size]; 例-chara[50];長さ50文字の文字列。 初期化 初期化は次のとおりです- 単一の文字を使用する 定数- char string[20] = { ‘H’, ‘i’, ‘l’, ‘l’, ‘s’ ,‘\0’} 文字列定数の使用- char string[20] = "H

  2. 文字列とそのアドレスへのポインタの配列を出力するCプログラム

    まず、Cプログラミング言語でのポインターの配列を理解しましょう。 ポインタの配列:(文字列へ) これは、要素が文字列の基本加算へのptrsである配列です。 次のように宣言および初期化されます- char *a[ ] = {"one", "two", "three"}; ここで、a[0]は文字列「one」の基本加算へのポインタです。 a [1]は、文字列「two」の基本加算へのポインタです。 a [2]は、文字列「three」の基本加算へのポインタです。 利点 ポインタの配列の利点を以下に説明します-