×

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

Remove duplicates from sorted array in C++

Remove duplicates from the sorted array in C++. This same process, due to a sorted array, is to erase the redundant components from the array. Examples: Input  : arr[] = {2, 2, 2,...

4 minutes read.

Program to convert infix to postfix expression in C++

Parentheses are frequently employed in mathematical formulas to make their interpretation easier to understand. However, with computers, parenthesis in an expression might lengthen the time it takes to find a...

7 minutes read.

C++ | C Plus Plus Data type

Data type in every language is very important. Data type means the different kinds of data that are supported by a particular programming language. A computer language’s data types specify the...

15 minutes read.

Multilevel Inheritance

C++ Multilevel Inheritance Multilevel inheritance is such an inheritance in which a derived class is created from another derived class. C++ Multilevel Inheritance Example In this example, a base class Student is inherited in...

2 minutes read.

Diamond Pattern 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...

5 minutes read.

How to make a password program in C++

Before understanding the password program, one must know about a password and why it is required. Password: A password is a word that permits access to somewhere or something. A password...

4 minutes read.

Static keyword in C++ vs Java

Both in C++ and Java, the static keyword is employed for essentially the same function. But there are some variations. The static keyword's similarities and differences between C++ and Java...

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.

Fast Input and Output in C++

In competitive programming, it's critical to read input as quickly as possible in order to save time. "Warning: Big I / O data, be aware of certain languages (but most...

3 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++ Infinite loop

The term "infinite loop" refers to a loop that does not terminate the loop according to the condition. In some cases, an infinite loop may be required in programming, or...

4 minutes read.

Program to arrange an array in alternate positive and negative numbers

Let’s say, there is given an array arr, arrange the array in such a way that every positive number is followed by a negative number. If there are extra positive...

4 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.

C++ Type Casting

The type casting of variables in the C++ programming language will be covered in this section. The term "type casting" describes how software converts one data type to another. There...

9 minutes read.

C++ Nested if

C++'s nested if statements enable more complex decision-making when a section of code needs to execute only after a set of conditions is met. The nested if control statement refers...

4 minutes read.

C++ Friend Functions

In C++, friends are special functions that are not a part of a class yet have access to its private and protected members. The friend keyword is used to declare...

4 minutes read.

C++ Reading file

In file handling, read() function is used to read data from the file into the program. The read() uses ifstream library to read data from a file. Syntax file-stream-class   file-stream-object;   file-stream-object.read((char *)&var , sizeof (var)); <h3">Example ofstream  outfile;         outfile . read((char*)&emp,sizeof(emp)); C++ File Handling read() Function Example Reading the content of existing...

2 minutes read.

Principles of Object-Oriented Programming in C++

What is Object-Oriented Programming? Object-oriented programming is about creating obejcts that represent the real-world entity . In object-oriented programming, objects are created for the class. One of the main objectives of...

5 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.

Default Constructor

C++ Default Constructor A default constructor is such constructor which does not take any parameter. A constructor initializes the data member when a class object created. If a programmer does not create any...

2 minutes read.