C Programming Functions and String Manipulation
5 Questions
0 Views

C Programming Functions and String Manipulation

Created by
@HonorableJasmine

Questions and Answers

What is the main difference between getchar() and getch() functions?

  • getchar() reads from the console, whereas getch() reads from a file
  • getchar() is used in C++, whereas getch() is used in C
  • getchar() waits for the Enter key to be pressed, whereas getch() does not (correct)
  • getchar() is a macro, whereas getch() is a function
  • What is the purpose of the srand() function?

  • To generate a random number
  • To convert a string to an integer
  • To get the current system time
  • To set the seed for the random number generator (correct)
  • What is the return type of the strlen() function?

  • void
  • int (correct)
  • string
  • char
  • What is the purpose of the strcmp() function?

    <p>To compare two strings</p> Signup and view all the answers

    How can you convert a command line argument from a string to an integer?

    <p>Using the atoi() function</p> Signup and view all the answers

    Study Notes

    Input Functions

    • getc() reads a character from the standard input (usually the keyboard) and returns it as an integer.
    • getchar() is a macro that calls getc() with stdin as the argument, making it equivalent to getc(stdin).
    • getch() is a non-standard function that reads a character from the standard input without echoing it to the console.
    • getche() is a non-standard function that reads a character from the standard input and echoes it to the console.

    Random Number Generation

    • rand() generates a random integer between 0 and RAND_MAX (a constant defined in ``).
    • srand() seeds the random number generator with an initial value, allowing rand() to produce a sequence of random numbers.

    Character Classification

    • isupper() returns a non-zero value if the character is an uppercase letter.
    • islower() returns a non-zero value if the character is a lowercase letter.

    String Manipulation

    strcpy()

    • Copies the string pointed to by the second argument into the array pointed to by the first argument.
    • Returns a pointer to the destination array.
    • Example: strcpy(dest, "hello"); copies the string "hello" into the array dest.

    strcat()

    • Appends the string pointed to by the second argument to the end of the string pointed to by the first argument.
    • Returns a pointer to the destination array.
    • Example: strcat(dest, "world"); appends the string "world" to the end of the string in dest.

    strlen()

    • Returns the length of the string pointed to by the argument.
    • Example: strlen("hello"); returns 5.

    strcmp()

    • Compares the string pointed to by the first argument to the string pointed to by the second argument.
    • Returns an integer indicating the result of the comparison: 0 if the strings are equal, less than 0 if the first string is less than the second, greater than 0 if the first string is greater than the second.
    • Example: strcmp("hello", "goodbye"); returns a value indicating that "hello" is less than "goodbye".

    Command Line Argument Conversion

    • To convert a command line argument from a string to an integer, use the atoi() function from ``.
    • Example: int arg = atoi(argv[1]); converts the first command line argument to an integer.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    This quiz covers the differences and usage of various C programming functions such as getc, getchar, getch, getche, rand, srand, isupper, islower, strcpy, strcat, strlen, and strcmp. It also includes converting command line arguments to integers.

    More Quizzes Like This

    Codehs Strings Functions Flashcards
    15 questions
    Python Functions and String Manipulation
    8 questions
    Stringr Functions Overview
    29 questions
    Use Quizgecko on...
    Browser
    Browser