0
0
Cprogramming~10 mins

String input and output in C - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the string stored in name.

C
#include <stdio.h>

int main() {
    char name[20] = "Alice";
    printf([1], name);
    return 0;
}
Drag options to blanks, or click blank then click option'
A"%s\n"
B"%d\n"
C"%c\n"
D"%f\n"
Attempts:
3 left
💡 Hint
Common Mistakes
Using %d or %c instead of %s for strings.
Forgetting to add quotes around the format specifier.
2fill in blank
medium

Complete the code to read a string input from the user into the array input.

C
#include <stdio.h>

int main() {
    char input[50];
    printf("Enter your name: ");
    scanf([1], input);
    printf("Hello, %s!\n", input);
    return 0;
}
Drag options to blanks, or click blank then click option'
A"%d"
B"%s"
C"%c"
D"%f"
Attempts:
3 left
💡 Hint
Common Mistakes
Using %d or %c instead of %s in scanf.
Adding & before the array name when reading strings.
3fill in blank
hard

Fix the error in the code to correctly read a string with spaces from the user.

C
#include <stdio.h>

int main() {
    char fullName[100];
    printf("Enter your full name: ");
    fgets([1], 100, stdin);
    printf("Hello, %s", fullName);
    return 0;
}
Drag options to blanks, or click blank then click option'
AfullName[100]
B&fullName
CfullName
D*fullName
Attempts:
3 left
💡 Hint
Common Mistakes
Using & before the array name with fgets.
Using array indexing like fullName[100] which is out of bounds.
4fill in blank
hard

Fill both blanks to create a string copy using strcpy.

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

int main() {
    char source[] = "Hello";
    char destination[20];
    [1](destination, [2]);
    printf("Copied string: %s\n", destination);
    return 0;
}
Drag options to blanks, or click blank then click option'
Astrcpy
Bstrncpy
Csource
Ddestination
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping source and destination arguments.
Using strncpy without specifying length.
5fill in blank
hard

Fill all three blanks to concatenate two strings safely using strncat.

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

int main() {
    char str1[20] = "Hello, ";
    char str2[] = "World!";
    [1](str1, [2], [3]);
    printf("Concatenated string: %s\n", str1);
    return 0;
}
Drag options to blanks, or click blank then click option'
Astrncat
Bstr2
C6
Dstrlen(str2)
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect length value for strncat.
Swapping the order of arguments.