Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String program help

Status
Not open for further replies.

sanman10535

Technical User
Mar 26, 2004
2
US
I have to write a function char *string_replace (char *s, char c, char r) that replaces every instance of character c in string s with character r, and returns the resulting string.

I have created the following main function to test my program. I can change the values sent to the function string_replace to test your program. I will be using my own strings to test the program; so my program should be able to work with any input string.

Can someone help me with the coding for this program? This is what I have so far as for the test program.
Code:
# include <stdio.h>

main () {
	
	char *str = “The Gators Are Going To Beat Florida State Every Year”;

	char *result = string_replace (str, ‘a’, ‘c’);

	printf (“%s”, result);

	result = string_replace (result, ‘c’, ‘a’);

	printf (“%s”, result);
}

The output should be:

The Gctors Are going to bect Floridc Stcte every yecr.
The Gators Are going to beat Florida State every year.
 
A clue for you
Code:
int i = 0;
for ( i = 0 ; s[i] != '\0' ; i++ )
That steps through the string one character at a time

> char *str = “The Gators Are Going To Beat Florida State Every Year”;
Even in your test program, you will want
char str[] = “The Gators Are Going To Beat Florida State Every Year”;
This is an initialised array (which can be modified)
You had a string constant, which is likely not modifyable.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top