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.