Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C strset() Function

Posted in C Tutorial
Updated on Oct 06, 2024
By Mari Selvan
👁ī¸ 366 - Views
âŗ 4 mins
đŸ’Ŧ 1 Comment
C strset() Function

Photo Credit to CodeToFun

🙋 Introduction

In the C programming language, string manipulation is a common and important aspect of coding.

The strset() function is one of the string manipulation functions provided by the C Standard Library. It is used to set every character of a string to a specified character.

In this tutorial, we'll explore the usage and functionality of the strset() function in C.

💡 Syntax

The syntax for the strset() function is as follows:

Syntax
Copied
Copy To Clipboard
char *strset(char *str, int character);
  • str: A pointer to the null-terminated string to be modified.
  • character: The character to set each element of the string to.

📄 Example 1

Let's dive into an example to illustrate how the strset() function works.

Warning: The strset() function is not a standard C library function, and its availability might vary across different compilers.

strset.c
Copied
Copy To Clipboard
#include <stdio.h>
#include <string.h>

int main() {
  char myString[] = "Hello, C Programming!";

  // Set every character in the string to '*'
  strset(myString, '*');

  // Output the modified string
  printf("Modified String: %s\n", myString);

  return 0;
}

đŸ’ģ Output

Output
Modified String: *********************

🧠 How the Program Works

In this example, the strset() function is used to set every character in the string "Hello, C Programming!" to '*'.

📄 Example 2

Here's an alternative example using a loop through each character in the string to the specified character.

strset.c
Copied
Copy To Clipboard
#include <stdio.h>

// Custom strset-like function
char * custom_strset(char * str, int character) {
  if (str == NULL) {
    // Handle null pointer case
    return NULL;
  }

  char * ptr = str; // Save the original pointer

  while ( * str != '\0') {
    * str = (char) character; // Set each character to the specified value
    str++;
  }

  return ptr; // Return the original pointer
}

int main() {
  char myString[] = "Hello, C Programming!";

  // Use the custom_strset function to set every character in the string to '*'
  custom_strset(myString, '*');

  // Output the modified string
  printf("Modified String: %s\n", myString);

  return 0;
}

đŸ’ģ Output

Output
Modified String: *********************

🧠 How the Program Works

In this program, custom_strset() is a custom function that behaves similarly to strset(). It takes a string and a character as parameters and sets each character in the string to the specified character. The program then uses this custom function to modify a string and prints the result.

↩ī¸ Return Value

The strset() function returns a pointer to the modified string. The returned pointer is the same as the pointer passed as the str argument.

📚 Common Use Cases

The strset() function is useful when you need to replace or initialize every character in a string with a specific character. It provides a quick and efficient way to perform bulk character assignments within a string.

📝 Notes

  • The strset() function operates on null-terminated strings. Ensure that the input string is properly null-terminated.
  • It is important to note that the strset() function does not change the size of the string; it only modifies the existing characters.

đŸŽĸ Optimization

The strset() function is optimized for setting multiple characters within a string efficiently. However, keep in mind that it operates on individual characters, so its efficiency is linear with the length of the string.

🎉 Conclusion

The strset() function in C is a valuable tool for manipulating strings by setting each character to a specified value. It is particularly handy for tasks that involve initializing or resetting string contents.

Feel free to experiment with different strings and characters to deepen your understanding of the strset() function. Happy coding!

👨‍đŸ’ģ Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
11 months ago

If you have any doubts regarding this article (C strset() Function) please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy