All Exams Test series for 1 year @ ₹349 only
Question

Which of the following function sets first n characters of a string to a given character?

The correct answer is

strnset ()

Understanding String Manipulation Functions in C/C++

String manipulation is a fundamental task in programming, involving operations like copying, concatenating, comparing, and modifying strings. C and C++ provide various built-in functions to perform these tasks efficiently. This question asks about a specific function used to initialize or set the first 'n' characters of a string to a particular character.

Analyzing the Options for Setting String Characters

Let's look at the provided options and determine which one fits the description of setting the first 'n' characters of a string to a given character:

  • strinit(): This is not a standard C or C++ library function for string manipulation. There might be functions with similar names in specific libraries or custom code, but it's not part of the standard string handling functions like those found in <string.h> or <cstring>.
  • strcset(): This function is also not a standard C or C++ library function. Standard libraries typically use names that are more descriptive or follow specific naming conventions (like `str` prefix for string functions).
  • strnset(): This is a standard C library function (often found in <string.h> or <cstring>) that is specifically designed to set the first 'n' characters of a string to a specified character. The 'n' in the name often indicates that the operation is limited to a specified number of characters.
  • strset(): This is another standard C library function. However, strset() sets *all* characters of a string (up to the null terminator) to a given character, not just the first 'n'.

Based on the standard C library functions, strnset() is the function that sets the first 'n' characters of a string to a given character.

Exploring the strnset() Function

The strnset() function is used to fill the initial portion of a string with a specific character. Here's a breakdown:

  • Purpose: To set the first specified number of characters of a string to a particular character.
  • Header File: Typically found in <string.h> or <cstring>. Note that this function is not part of the C89, C99, or C11 standards, nor C++ standards. It is commonly available in some compilers (like those for MS-DOS, Windows) as part of older libraries or extensions. A more standard way to achieve similar behavior is using memset(). However, given the options, we are considering the function as it is presented.
  • Syntax: A common syntax is often presented as:

    char *strnset(char *str, int c, size_t n);

  • Parameters:
    • str: A pointer to the string that will be modified.
    • c: The character (passed as an int) to set the string elements to.
    • n: The maximum number of characters to set. The function sets at most n characters or until the null terminator is reached, whichever comes first.
  • Return Value: It returns a pointer to the modified string str.

Example of strnset() Usage

Let's look at an example to see how strnset() works:

#include <stdio.h>
#include <string.h> // Or <cstring>

int main() {
    char myString[20] = "Hello World";
    printf("Original string: %s\n", myString);

    // Set the first 5 characters to '*'
    strnset(myString, '*', 5);
    printf("After strnset(*, 5): %s\n", myString); // Output: ***** World

    char anotherString[20] = "Programming";
    printf("Original string: %s\n", anotherString);

    // Set the first 10 characters to 'X'
    strnset(anotherString, 'X', 10);
    printf("After strnset('X', 10): %s\n", anotherString); // Output: XXXXXXXXXXing

    // Set the first 20 characters (more than string length) to 'Y'
    char shortString[10] = "Test"; // Buffer size 10
    printf("Original string: %s\n", shortString);
    // Be cautious: n should not exceed the buffer size.
    // If n is larger than the string length, it might write beyond the null terminator
    // up to n characters or until the buffer size limit is hit, which can be dangerous.
    // Standard documentation sometimes states it sets 'n' characters *or* until null is found.
    // Let's assume the behaviour described in the question (sets first n).
    // Using n=7 for a buffer of 10 for a string "Test\0"
    strnset(shortString, 'Y', 7);
    printf("After strnset('Y', 7): %s\n", shortString); // Output: YYYYYYYt

    return 0;
}

This example demonstrates how strnset() modifies the initial part of the string with the specified character.

Comparison with strset()

It's useful to compare strnset() with strset():

Feature strnset() strset()
Characters Modified First 'n' characters All characters up to the null terminator
Limit Limited by 'n' or null terminator Limited by null terminator
Syntax Example strnset(str, char, n); strset(str, char);
Standard Status Non-standard, but commonly available in some environments Non-standard, but commonly available in some environments

As you can see, strnset() provides more granular control by allowing you to specify exactly how many characters from the beginning of the string should be modified.

Conclusion

The function that sets the first 'n' characters of a string to a given character is strnset(). While not part of the official C/C++ standards, it is a widely available function in many compiler implementations and directly matches the description given in the question.

Revision Table: String Setting Functions

Function Name Description Standard?
strnset() Sets the first 'n' characters of a string to a specific character. Non-standard (common extension)
strset() Sets all characters of a string (up to null) to a specific character. Non-standard (common extension)
memset() Sets the first 'n' bytes of a memory block to a specific byte value. Can be used for strings. Standard (<string.h> or <cstring>)

Additional Information: Standard String Manipulation

While strnset() and strset() are convenient and available in many systems, it's good practice to be aware of standard functions for portability. For setting the first 'n' characters, the standard C library function memset() is often used. It operates on raw memory bytes, so you can use it to set a character in the first 'n' positions of a character array (string).

The syntax for memset() is:

void *memset(void *s, int c, size_t n);

Here, s is a pointer to the memory area (your string), c is the byte value (character) to set, and n is the number of bytes (characters) to set.

For example, to set the first 5 characters of myString to '*' using memset():

#include <string.h> // For memset

char myString[20] = "Hello World";
memset(myString, '*', 5); // Sets the first 5 bytes to '*'

This achieves the same result as strnset() for setting the initial characters, but it's a standard function.

Was this answer helpful?

Important Questions from Programming in C

  1. Which among the following is a valid string function?

  2. A function which calls itself is called a

  3. Which of the following statement provides an easy way to dispatch execution to different parts of code based on the value of the expression?

  4. Which of the following operators has left to right associativity?

  5. Which of the following is not a keyword in 'C'?

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App