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;
}

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.

@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!! :) :) :)

commented: Bad suggestion. -2

Thanks for helping

@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

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.