C Programming String Functions
5 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What does the function strcat(dest, src) do?

  • Compares two strings and returns 0 if equal
  • Copies the string `src` to `dest`
  • Returns a pointer to the first occurrence of character `c` in `str`
  • Concatenates `src` to the end of `dest` (correct)

What is the purpose of the strupr(str) function?

  • Converts a string to lowercase
  • Reverses a string
  • Converts a string to uppercase (correct)
  • Returns the length of the string

Which function is used to compare two strings?

  • strcat
  • strcpy
  • strupr
  • strcmp (correct)

What is the result of the code sprintf(str, "Hello, %s!", "World")?

<p>Hello, World! (C)</p> Signup and view all the answers

What is the purpose of the strrev(str) function?

<p>Reverses a string (B)</p> Signup and view all the answers

Study Notes

String Functions

  • Library: ``
  • Functions:
    • strlen(str): returns the length of the string
    • strcpy(dest, src): copies the string src to dest
    • strcat(dest, src): concatenates src to the end of dest
    • strcmp(str1, str2): compares two strings, returns 0 if equal
    • strchr(str, c): returns a pointer to the first occurrence of character c in str
    • strstr(str1, str2): returns a pointer to the first occurrence of str2 in str1

String Manipulation

  • Functions:
    • strupr(str): converts a string to uppercase
    • strlwr(str): converts a string to lowercase
    • strupr(str): converts the first character of a string to uppercase
    • strrev(str): reverses a string
  • Note: These functions are not part of the standard `` library, but are commonly used in string manipulation.

String Concatenation

  • Using strcat():
    • strcat(str1, str2): concatenates str2 to the end of str1
    • Example: strcat("Hello, ", "World!") results in "Hello, World!"
  • Using array indexing:
    • Example: char str[20]; strcpy(str, "Hello"); str[5] = ','; strcat(str, " World!"); results in "Hello, World!"
  • Using sprintf():
    • sprintf(str, "format string", arguments): formats a string using the format string and arguments
    • Example: sprintf(str, "Hello, %s!", "World") results in "Hello, World!"

String Functions

  • strlen(str) returns the length of the string.
  • strcpy(dest, src) copies the string src to dest.
  • strcat(dest, src) concatenates src to the end of dest.
  • strcmp(str1, str2) compares two strings and returns 0 if they are equal.
  • strchr(str, c) returns a pointer to the first occurrence of character c in str.
  • strstr(str1, str2) returns a pointer to the first occurrence of str2 in str1.

String Manipulation

  • strupr(str) converts a string to uppercase.
  • strlwr(str) converts a string to lowercase.
  • strupr(str) converts the first character of a string to uppercase.
  • strrev(str) reverses a string.
  • Note: These functions are not part of the standard library, but are commonly used in string manipulation.

String Concatenation

  • strcat(str1, str2) concatenates str2 to the end of str1.
  • Example: strcat("Hello, ", "World!") results in "Hello, World!".
  • Concatenation using array indexing: Example: char str; strcpy(str, "Hello"); str[5] = ','; strcat(str, " World!"); results in "Hello, World!".
  • sprintf(str, "format string", arguments) formats a string using the format string and arguments.
  • Example: sprintf(str, "Hello, %s!", "World") results in "Hello, World!".

Studying That Suits You

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

Quiz Team

Description

This quiz covers the string functions in C programming, including strlen, strcpy, strcat, strcmp, strchr, and strstr. Test your knowledge of string manipulation in C.

More Like This

PHP String Functions Quiz
10 questions

PHP String Functions Quiz

DistinguishedFern avatar
DistinguishedFern
C Programming String Functions
10 questions

C Programming String Functions

SensationalRisingAction avatar
SensationalRisingAction
PHP String Functions Quiz
6 questions
C Programming Fundamentals Quiz
48 questions
Use Quizgecko on...
Browser
Browser