Ok so I posted this before but didn't get a response...
Our prof gave us this problem:
"Write a program that will get, from the user, a string of characters such as a sentence, and place this into a declared char array. Ask the user which letter they would like to replace, then ask the user to enter a letter that they would like to replace it with."
So I did something like this:
#include <iostream>
#include <cstring>
void main(void)
{
const int charString = 20;
char sentenceHolder1[charString];
char sentenceHolder2[charString];
int index1;
int index2;
int index3;
char replacement;
char exitOnInput;
int count = 0;
cout << "Enter a sentence of up to 20 characters" << endl;
cin.getline(sentenceHolder1, charString);
cout << "Enter a character that you would like to be replaced" << endl;
cin.getline(sentenceHolder2, charString);
cout << "Enter what you would like it to be replaced with" << endl;
cin >> replacement;
for(index1=0; index1 < charString; index1++)
{
for(index2=0; index2 < charString; index2++)
{
while ( sentenceHolder1[index1] == sentenceHolder2[index2] )
{
count = count+1;
sentenceHolder1[index1] = replacement;
}
}}
for ( index3 = 0; index3 < charString; index3++)
{
cout << sentenceHolder1[index3];
}
cout << "Enter a letter followed by 'enter' to exit" << endl;
cin >> exitOnInput;
return 0;
}
It kind of works, but I know that there are some mistakes which I am fixing...
What I want to know is this: How do I replace a letter in the array with another letter that is already stored in the array? This is not what we were asked but i'm wondering how you would go about implementing such a thing.
Example: User enters Hello world, replace the 7th word in the array with the first word entered.