Print the given 3 string after modifying and concatenating

This program demonstrates how to modify multiple strings by replacing all characters with user−specified replacement characters, then concatenate the modified strings together.

Syntax

strlen(string)          // Get string length
strcat(dest, source)    // Concatenate strings
scanf("%s", string)     // Read string input

Algorithm

START
Step 1 ? Declare three character arrays str1, str2, str3 and replacement characters ch1, ch2, ch3
Step 2 ? Input the three strings and three replacement characters
Step 3 ? Replace all characters in str1 with ch1 using loop
Step 4 ? Replace all characters in str2 with ch2 using loop  
Step 5 ? Replace all characters in str3 with ch3 using loop
Step 6 ? Display all three modified strings
Step 7 ? Concatenate str1 with str2 and store result in str1
Step 8 ? Concatenate str1 with str3 and store result in str1
Step 9 ? Display the final concatenated string
STOP

Example

Here's a complete program that replaces characters in three strings and concatenates them −

#include <stdio.h>
#include <string.h>

int main() {
    char str1[1000], str2[1000], str3[1000], ch1, ch2, ch3;
    int i;
    
    printf("Enter three strings:<br>");
    printf("String 1: ");
    scanf("%s", str1);
    printf("String 2: ");
    scanf("%s", str2);
    printf("String 3: ");
    scanf("%s", str3);
    
    printf("\nEnter replacement characters:<br>");
    printf("Character for string 1: ");
    scanf(" %c", &ch1);
    printf("Character for string 2: ");
    scanf(" %c", &ch2);
    printf("Character for string 3: ");
    scanf(" %c", &ch3);
    
    /* Replace all characters in each string */
    for (i = 0; i < strlen(str1); i++) {
        str1[i] = ch1;
    }
    for (i = 0; i < strlen(str2); i++) {
        str2[i] = ch2;
    }
    for (i = 0; i < strlen(str3); i++) {
        str3[i] = ch3;
    }
    
    printf("\nModified strings:<br>");
    printf("String 1: %s<br>", str1);
    printf("String 2: %s<br>", str2);
    printf("String 3: %s<br>", str3);
    
    /* Concatenate all strings */
    strcat(str1, str2);
    strcat(str1, str3);
    
    printf("\nAfter concatenation: %s<br>", str1);
    
    return 0;
}

Output

Enter three strings:
String 1: tutorials
String 2: point
String 3: best

Enter replacement characters:
Character for string 1: x
Character for string 2: y
Character for string 3: z

Modified strings:
String 1: xxxxxxxxx
String 2: yyyyy
String 3: zzzz

After concatenation: xxxxxxxxxyyyyyzzzz

Key Points

  • The program uses strlen() to determine string lengths for replacement loops.
  • Each character in the original strings is replaced with the corresponding replacement character.
  • The strcat() function concatenates strings by appending the source to the destination.
  • Space before %c in scanf(" %c", &ch1) consumes any whitespace characters.

Conclusion

This program effectively demonstrates string manipulation in C by replacing characters and concatenating strings. The combination of loops for character replacement and strcat() for concatenation provides a practical example of string processing.

Updated on: 2026-03-15T11:04:06+05:30

301 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements