Hi, can anyone help me with this program.

the program inputs a string and replace the first char with the second
one.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX 200

int main(void)
{
	char myString[MAX];
	char getCh;
	char setCh;
	int i = 0;

	printf("Please enter the string: ");
	gets(myString);
	
	printf("Input first char: ");
	scanf("%c", &getCh);

	printf("input second char: ");
	scanf("%c", &setCh);

	for(i=0; i<strlen(myString); i++)
		if(myString[i] == getCh)
			myString[i] = setCh;
	printf("Final result: %s\n", myString);

	return 0;
}

Edited 6 Years Ago by WaltP: Added CODE Tags

Don't and I mean don't ever use

gets(myString);

Read Please
BUGS
Never use gets(). Because it is impossible to tell without knowing the
data in advance how many characters gets() will read, and because
gets() will continue to store characters past the end of the buffer, it
is extremely dangerous to use. It has been used to break computer
security. Use fgets() instead.

Edited 6 Years Ago by gerard4143: n/a

@noobuser:
scanf() leaves the newline character in the input buffer, that's why your code doesn't work the way you intended, this page describes how to fix that.

Don't and I mean don't ever use

gets(myString);

Read Please
BUGS
Never use gets(). Because it is impossible to tell without knowing the
data in advance how many characters gets() will read, and because
gets() will continue to store characters past the end of the buffer, it
is extremely dangerous to use. It has been used to break computer
security. Use fgets() instead.

I will add to that "Never use scanf("%s",...) and scanf("%c",...) ". See this series for an explanation why.

Hi Friend!

I believe that Mr. Walt is correct in this matter.

I would suggest that you use "getch" instead you will find it much to your liking.

Have a healthy day and God Bless!! :) :) :)

Comments
Bad suggestion.

@noobuser:
scanf() leaves the newline character in the input buffer, that's why your code doesn't work the way you intended, this page describes how to fix that.

Excellent

This question has already been answered. Start a new discussion instead.