Home  >  Article  >  Backend Development  >  C++ program: replace character at specific index

C++ program: replace character at specific index

WBOY
WBOYforward
2023-08-25 22:53:091091browse

C++ program: replace character at specific index

A string is a set of characters. We can also call them character arrays. considering A character array consisting of strings with the specified index and value. sometimes We can make some modifications to the string, one of the modifications is to replace characters By providing a specific index. In this article we will see how to replace a character from a specific index inside a string using C .

grammar

String_variable[ <given index> ] = <new character>

In C, we can access string characters using indexes. The code used here to replace a character is At the specified index position, we only need to assign the position to a new character character as shown in the syntax. Let us see the algorithm for a better understanding.

algorithm

  • Get the string s, specify the index i, and a new character c
  • If index i is positive and its value does not exceed the string size, then
    • s[ i ] = c
    • return s
  • The Chinese translation of
  • otherwise is: Otherwise
    • Return s without changing anything
  • end if

Example

#include <iostream>
using namespace std;
string solve( string s, int index, char new_char){
   
   // replace new_char with the existing character at s[index]
   if( index >= 0 && index < s.length() ) {
      s[ index ] = new_char;
      return s;
   } else {
      return s;
   }
}
int main(){
   string s = "This is a sample string.";
   cout << "Given String: " << s << endl;
   cout << "Replace 8th character with X." << endl;
   s = solve( s, 8, 'X' );
   cout << "Updated String: " << s << endl;
   cout << "Replace 12th character with ?." << endl;
   s = solve( s, 12, '?' );
   cout << "Updated String: " << s << endl;
}

Output

Given String: This is a sample string.
Replace 8th character with X.
Updated String: This is X sample string.
Replace 12th character with ?.
Updated String: This is X sa?ple string.

in conclusion

Replacing characters at a specified index is simple enough in C . C strings are mutable, so we can change them directly. In some other languages ​​like java, the strings are not mutable. There is no range in which characters can be replaced by assigning new characters In such cases, a new string needs to be created. A similar will happen if we define strings as In C language, we can use character pointers. In our example, we define a function to replace a Returns the character at the given index position. If the given index is out of range, it will return string and it will remain unchanged.

The above is the detailed content of C++ program: replace character at specific index. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete