Complementary task for topic: 5

M Nemeth · 2023-08-29 15:21:04.619219'

Simple strings: I/O, String Concatenation

Simple strings: I/O, String Concatenation

Write a C program that uses scanf() to read two strings from the user and then concatenates to be displayed using printf(). (no strcat() needed!)

Hint: Strings are character arrays, it is terminated by "End-of-String" character, but now you do not need to use this, as scanf() will take this character to the end of the string. The array size is usually not equal to the number of required characters. That is not a problem.
Trick, print two string after each other makes them concatenate

Solution
#include 

int main() {
    char str1[100], str2[100];

    printf("Enter the first string: ");
    scanf("%99[^\n]", str1); // Read the first string with spaces

    printf("Enter the second string: ");
    scanf(" %99[^\n]", str2); // Read the second string with spaces


    printf("Concatenated string: %s%s\n", str1,str2);

    return 0;
}



Explanation

< < previous    next > >