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.

Recommended Answers

All 12 Replies

Better yet - how to replace all instances of a certain letter in the array ( lets say L ) with the first letter in the array.

line 9: If you want someone to type up to 20 characers then you have to define charString as 21 in order to allow room for the null string terminating character.

line 23: All you want here is to enter a single character, not an entire string. Similar to line 26.

lines 29-42: all you need here is just one loop to test each character in sentenceHolder1 for the character you enter on line 23 and possibly replace it with the replacement character.

int len = strlen(sentenceHolder1);
for(int i = 0; i < len; i++)
{
    if( sentenceHolder1[i] == look_for_character )
        sentenceHolder1[i] = replacement_character;
}

Stick to the program requirements and don't try to do fancy stuff. Your task is to code what the prof asked you to code, not to make up stuff on your own.

There isn't anything wrong with trying out various things on your own -- just don't hand any of it in as part of the assignment.

Thanks for that. As I said I know that there are several errors in this code, thanks for helping fix that one.

Any help with how to replace an instance of a character in the array with the first character of the array would be greatly appreciated :)

Stick to the program requirements and don't try to do fancy stuff. Your task is to code what the prof asked you to code, not to make up stuff on your own.

There isn't anything wrong with trying out various things on your own -- just don't hand any of it in as part of the assignment.

Yea I wasn't planning on handing that part in, my prof just suggested in one of our classes that it was possible and I was wondering how you could do it because he won't tell me.

Any help with how to replace an instance of a character in the array with the first character of the array would be greatly appreciated :)

sentenceHolder1[i] = sentenceHolder2[0]; will replace a character in sentenceHolder1 with the first charcter in sentenceHolder2.

>>Yea I wasn't planning on handing that part in, my prof just suggested in one of our classes that it
>>was possible and I was wondering how you could do it because he won't tell me.

Yes it is possible. Hint: strstr() from string.h is your friend when working with charcter arrays. There are better ways when using c++ std::string class, but you probably have not gotton to those in your class.

Thanks for that. As I said I know that there are several errors in this code, thanks for helping fix that one.

Any help with how to replace an instance of a character in the array with the first character of the array would be greatly appreciated :)

int len = strlen(sentenceHolder1);
for(int i = 0; i < len; i++)
{
    if( sentenceHolder1[i] == look_for_character )
        sentenceHolder1[i] = sentenceHolder1[0]; //I think this will be OK
}

maybe, the following code will help you.

#include <iostream>
#include <cstring>

using namespace  std;

int main(int argc, char *argv[])
{

  char sentenceHolder1[128];
  char toReplace;
  int index1;
  char replacement;
  char exitOnInput;
	
	cout << "Enter a sentence: " << endl;
	cin.getline(sentenceHolder1, sizeof(sentenceHolder1) );
	
	cout << "Enter a character that you would like to be replaced" << endl;
	cin >> toReplace;
	
	cout << "Enter what you would like it to be replaced with" << endl;
	cin >> replacement;
	
	
	for(index1=0; index1 < strlen(sentenceHolder1); index1++)
	{
    if (sentenceHolder1[index1] == toReplace)
    {
      sentenceHolder1[index1] = replacement;
    }
  }
  
  cout<<sentenceHolder1<<endl;
  
  cout << "Enter a letter followed by 'enter' to exit" << endl;
  cin >> exitOnInput;
  
  return 0;
}

That's the same as I posted, but this: for(index1=0; index1 < strlen(sentenceHolder1); index1++) You don't want to call strlen() like that because it has to be executed on every iteration of the loop. Code it once as I did and just use that variable.

Well thank you both, I'm sure I can figure it all out now.

#include<stdio.h>

void main()
{
char a[25] = "kaveyarrasan.k";
char c;
int i,n;
printf("Enter the position or place of char to b replace:"\n);
scanf("%d",&n);
for(i=0;i<=n;i++)
{
if(i==n)
{
printf("Enter the Character to replace"\n);
scanf("%c",&c)
a = c;
}
}
for(i=0;i<=25;i++)
printf("After replacing the character the string will be %c",a);
}

commented: Why so eager to show off bad code? -2

List of things wrong with kaveyarrasan.k's effort:
- it's two years too late
- it's in C, not C++ (see the forum title)
- it has no code tags
- it uses void main
- it has a buffer overflow (<=25 steps off the end)
- the for loop to "search" for the char to replace serves no purpose. Just use a[n] directly.
- as the input buffer is un-flushed, the %c scanf will read the \n left behind by the %d scanf earlier.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.