Home > Backend Development > C++ > body text

C++ program: replace character at specific index

WBOY
Release: 2023-08-25 22:53:09
forward
1146 people have browsed it

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>
Copy after login

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;
}
Copy after login

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.
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template