Bird
Raised Fist0
Cprogramming~10 mins

String input and output in C - Interactive Code Practice

Choose your learning style10 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.

Practice

(1/5)
1. Which function is used to read a single word string from the user in C?
easy
A. fgets(str, size, stdin);
B. printf("%s", str);
C. gets(str);
D. scanf("%s", str);

Solution

  1. Step 1: Understand string input functions

    scanf with "%s" reads a single word string until whitespace.
  2. Step 2: Identify correct function for single word input

    printf is for output, gets is unsafe, fgets reads whole line including spaces.
  3. Final Answer:

    scanf("%s", str); -> Option D
  4. Quick Check:

    Single word input = scanf("%s", str); [OK]
Hint: Use scanf with %s for single word input [OK]
Common Mistakes:
  • Using printf instead of scanf for input
  • Using fgets expecting single word input
  • Using gets which is unsafe
2. Which of the following is the correct syntax to print a string variable str in C?
easy
A. printf("%d", str);
B. printf("%s", str);
C. scanf("%s", str);
D. gets(str);

Solution

  1. Step 1: Identify the print format specifier for strings

    "%s" is used in printf to print strings.
  2. Step 2: Match correct function and format

    printf with "%d" is for integers, scanf is for input, gets reads input.
  3. Final Answer:

    printf("%s", str); -> Option B
  4. Quick Check:

    Print string = printf("%s", str); [OK]
Hint: Use printf with %s to print strings [OK]
Common Mistakes:
  • Using %d to print strings
  • Using scanf instead of printf for output
  • Using gets for printing
3. What will be the output of the following C code?
#include <stdio.h>
int main() {
    char str[20];
    scanf("%s", str);
    printf("%s", str);
    return 0;
}

If the user inputs: Hello World
medium
A. Hello
B. Hello World
C. World
D. Compilation error

Solution

  1. Step 1: Understand scanf with %s behavior

    scanf("%s", str) reads input until first whitespace, so it reads only "Hello".
  2. Step 2: Output printed by printf

    printf("%s", str) prints the stored string, which is "Hello".
  3. Final Answer:

    Hello -> Option A
  4. Quick Check:

    scanf with %s stops at space = Hello [OK]
Hint: scanf %s reads only until space, not full line [OK]
Common Mistakes:
  • Expecting scanf %s to read full line with spaces
  • Confusing scanf with fgets
  • Assuming output includes 'World'
4. Identify the error in the following code snippet for reading a full line including spaces:
#include <stdio.h>
int main() {
    char str[50];
    scanf("%s", str);
    printf("%s", str);
    return 0;
}
medium
A. char array size is too large
B. printf("%s", str) cannot print strings
C. scanf("%s", str) reads only until first space, not full line
D. Missing return type for main

Solution

  1. Step 1: Analyze scanf with %s for input

    scanf("%s", str) stops reading input at first whitespace, so it cannot read full line with spaces.
  2. Step 2: Check other parts of code

    printf("%s", str) correctly prints string, array size is sufficient, main has return type int.
  3. Final Answer:

    scanf("%s", str) reads only until first space, not full line -> Option C
  4. Quick Check:

    scanf %s stops at space, so not full line input [OK]
Hint: Use fgets to read full line with spaces [OK]
Common Mistakes:
  • Assuming scanf %s reads full line
  • Thinking printf can't print strings
  • Confusing array size issues
5. How can you correctly read a full line of text including spaces into a string str of size 100 in C?
hard
A. fgets(str, 100, stdin);
B. scanf("%s", str);
C. printf("%s", str);
D. gets(str);

Solution

  1. Step 1: Understand functions for reading strings with spaces

    scanf("%s", str) reads only until space, gets is unsafe and deprecated.
  2. Step 2: Identify safe function to read full line

    fgets(str, 100, stdin) reads up to 99 characters including spaces safely.
  3. Final Answer:

    fgets(str, 100, stdin); -> Option A
  4. Quick Check:

    Use fgets for full line input with spaces [OK]
Hint: Use fgets to read lines with spaces safely [OK]
Common Mistakes:
  • Using scanf %s for full line input
  • Using gets despite safety issues
  • Confusing printf with input functions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard