×

Memset in C++

Memset () is a function in the C++ programming language that fills memory blocks.

  • The value of "ch" is first converted to an unsigned character. In this case, "ch" denotes the character that will be filled with another value from the memset () function.
  • The letter "ch" is then copied to the first characters "n" of the object when st [points].
  • In this context, "n" refers to the block size, specified in memset (), and must be equal to or less than the size of the object identified by st [].
  • The effect cannot be explained if the value of n is greater than the size of the object mentioned st [].
  • The function will behave the way you did in the previous example, that is, unspecified, if the situation sometimes occurs when an object cannot be copied.
  • The cstring title file contains a memset () function in the C ++ editing language; without mentioning this header file, you will not be able to use memem () method.

Example1:

// Program to implement memset ( ) function in C++  
#include < iostream >  
#include < bits/stdc++.h >
#include < stdio >
#include < sdtlib >
#include < cstring >      // header file that contains the memset ( ) function, without   
using namespace std ;  // we cannot able to access the use of memset ( ) function
int main ( )  
{  
    char st [ ] = " TutorialAndexample " ;  
    char ch ;  
    cout << " Printing the given string : " << st << endl ;  
    cout << " enter an alphabet from which you want to replace the each charater of the         given string : " ;  
    cin >> ch ;  
    memset ( st , ch , sizeof ( st ) ) ;  
    cout << " Printing the string after replacing the each character of the string with the given substitute : " << endl ;  
    cout << " " << st ;  
    return 0 ;  
}

OUTPUT:

Print the given string: TutorialAndexample
enter an alphabet from which you want to replace the each character of the given string : a
Printing the string after replacing the each character of the string with the given substitute : aaaaaaaaaaaaaa
………………………………………………………
Process executed in 1.11 seconds
Press any key to continue

Explanation

In the aforementioned example, we saw how each letter in the string "JavaTpoint" was changed into the single alphabet supplied by the user. In this case, the space is also regarded as one character.

Example2:

// Program to implement memset ( ) function in C++  
#include < iostream > 
#include < bits/stdc++.h >
#include < stdlib >
#include < stdio > 
#include < cstring >    // header file that contains the memset ( ) function, without it   
using namespace std ;  //we cannot able to access the use of memset ( ) function 
int main()  
{  
    int arr [ 100 ] ;  
    int n , i , p ;  
    cout << " enter size of an integer array: " ;   
    cin >> n ;  
    cout << " enter an array elements: " << endl ;  
    for ( i = 0 ; i < n ; i + + )  
    {  
        cin >> arr [ i ] ;  
    }  
    cout << " Printing the given array : " << endl ;  
    for ( i = 0 ; i < n ; i + + )  
    {  
        cout << " " << arr [ i ] << endl ;  
    }  
    cout << " enter an integer value from which you want to replace the each charater of the given string :  " ;  
    cin >> p ;  
    memset ( arr , p , sizeof ( arr ) ) ;  
    cout << " Printing the array after replacing the each value of the array with the given substitute : " << endl ;  
    for ( i = 0 ; i < n ; i + + )  
    {  
        cout << " " << arr [ i ] << endl ;  
    }  
    return 0 ;  
}

OUTPUT:

enter size of an integer array: 6
enter an array elements: 
12
11
10
55
34
321
Printing the given array:
12
11
10
55
34
321
enter an integer value from which you want to replace the each charater of the given string : 1
Printing the array after replacing the each value of the array with the given substitute :
1
1
1
1
1
1
……………………………..
Process executed in 4.33 seconds
Press any key ton continue.

Explanation

Here, the use of just the integer values 0 and -1 is significant, meaning that all characters in the string must be changed to these values in order to avoid producing trash. If you enter 2, then all the characters in the string have been replaced by a garbage value.


Related Topics

Array of Vectors in C++ STL

Prerequisites: C++ STL Arrays and C++ STL Vector. A group of items kept in consecutive memory region is known as an array. It is to group similar objects of the same...

4 minutes read.

C++ Math Functions

C++ Math Functions: Like other programming languages, C++ offers plenty of mathematical functions needed for various purposes. These functions are defined mainly in the math library in C++. Let us...

4 minutes read.

Similarities between C++ and Java

People who are preparing for software engineering roles must have come across either one of these languages because as all the companies do not accept python for hiring a fresher...

3 minutes read.

Structured Binding in C++

Structured binding is the new feature of C++ 17. It is used to bind the specified name with an element of the initializer. Structure binding is used to declare multiple...

3 minutes read.

C++ | C Plus Plus While loop

In this article, we will discuss the C++ while loop with its syntax, use, key features, key points, pseudo code, and examples. What is the While Loop? The “while loop” is a...

4 minutes read.

Depth First Search Program to Traverse a Graph in C++

Depth First Search (DFS) is a technique that is used for transversing the graph. The process of Depth First Search (DFS) starts from the root node and then next comes...

6 minutes read.

Snake Code in C++

Snake is a popular game that can be played on almost any device and runs on any operating system. In this game, snakes can move in any direction, including left,...

4 minutes read.

Armstrong Number using While Loop in C++

What is while Loop? A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the...

4 minutes read.

C++ Switch

In C++, an expression or variable can be tested against a range of constant values using a switch statement, which is a control flow statement. It offers a productive method...

4 minutes read.

Memset in C++

Memset () is a function in the C++ programming language that fills memory blocks. The value of "ch" is first converted to an unsigned character. In this case, "ch" denotes the...

3 minutes read.

Function Pointer in C++

Definition: A function pointer in C++ is the same as a usual pointer which is used to point some variables. Function pointers are used to point functions or say store the...

4 minutes read.

Input Iterators in C++

What are input iterators? Input iterators are used in sequence for carrying out input operations where each value is read-only. It is pointed by the iterator and further incremented. All the iterators...

4 minutes read.

Binary Operator Overloading in C++

The Binary Operator Overloading in the C++ programming language will be covered in this part. An operator which comprises two operands to execute a mathematical operation is termed the Binary...

6 minutes read.

INT_MAX and INT_MIN in C/C++

In competitive programming, assigning a variable that maximum or minimum value a data type can carry is frequently necessary. Still, it might be challenging to recall such a significant, exact...

3 minutes read.

Const_cast in C++ Type Casting Operators

In this tutorial, we will learn enough about const cast in the most widely used programming language, C++, as well as type casting operations. C++ supports the four types of casting...

4 minutes read.

C++ Algorithms

There are plenty of programming paradigms that are closely associated with the implementations of code and simulate them into a proper functional one. This is done with the help of...

5 minutes read.

Compile Time Polymorphism in C++

What is Polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. One application of polymorphism in...

4 minutes read.

Top best IDEs for C/C++ Developers in 2024

Nothing in the current digital world is conceivable without programming. Everything needs programming, from the cell phones in our pockets to self-driving cars. Programming is also necessary for the mouse...

9 minutes read.

Decimal to Octal in C++

We must create a software that converts a decimal number into an equal octal number given a decimal number as input i.e. convert a number having a base value of...

3 minutes read.

How to Use Getline in C++

What is getline in C++? Similar to the cin function, which allows the programmer to take input from the user getline() function also takes input from the user. But in this...

4 minutes read.