ahmed0saber

Change characters in a string in C++

Apr 3rd, 2021
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.34 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std ;
  3. int main()
  4. {
  5.     string Num ;
  6.     char a , b ;
  7.     cout<<"Enter the number : ";
  8.     cin>>Num;
  9.     cout<<"Enter the digit you want to change : ";
  10.     cin>>a;
  11.     cout<<"Enter the digit to use instead : ";
  12.     cin>>b;
  13.     for(int i=0 ; i<Num.length() ; i++)
  14.     {
  15.         if(Num[i]==a)
  16.         {
  17.             Num[i]=b;
  18.         }
  19.     }
  20.     cout<<Num;
  21. }
Advertisement
Add Comment
Please, Sign In to add comment