Index

closed account (9ECjNwbp)
am working on project
that i Need to change index in string but without using replace
any suggestion?
closed account (9ECjNwbp)

cout << "\n What is the index of the character you want to change? ";
cin >> ind;
if (a[ind] == '\0')
cout << "That position is outside the bounds of the string. String unchanged.";

cout << "\nWhat character do you want in that position? ";
char New;
cin >> New;

i got this far
Are you using C-style strings (char[] str = "Hello World") or C++ strings (std::string str = "Hello World") ? edit: Question is irrelevant.

if (a[ind] == '\0')
What happens if the index is outside the bounds of the string?

i got this far

You're almost there! The solution is actually much easier than you think.
1
2
3
4
5
6
7
8
// Let's say you have an array of integers.
int arr[] = { 2, 7, 9, 3, 8 };
// How would you change the integer at index 2 of this array?

/* 
A string is an array of characters. Applying the same logic as above,
how would you replace the nth index of the string?
*/
Last edited on
closed account (9ECjNwbp)
if (a[ind] == '\0') the program will run again and ask new q!
my question is how I change it if I don't know which index the user will enter?
int array [3] = to new index that if i know exactly which index
closed account (9ECjNwbp)
c++ string

that i Need to change index in string but without using replace
1
2
cout << "\n What is the index of the character you want to change? ";
cin >> ind;


my question is how I change it if I don't know which index the user will enter?


You have contradicting questions.
closed account (9ECjNwbp)
array[ind]=new

I got it Thank U
Topic archived. No new replies allowed.