By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,109 Members | 1,007 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,109 IT Pros & Developers. It's quick & easy.

this won't compile

P: 11
***instructions: write a function sreplace( ) with the prototype:
void sreplace(char newc, char oldc, char *s);
which will replace all occurances of the character oldc with the character newc in the string s. there is no return value so merely do a return***

I *think* this code makes sense. but it wont compile. please help. thank you.


void sreplace(char newc, char oldc, char *s){

while(char *s != EOF){
*s== oldc;
if(*s == newc){
oldc=newc;
*s++;
}
}
}
return;
Sep 17 '06 #1
Share this Question
Share on Google+
5 Replies


Banfa
Expert Mod 5K+
P: 8,916
Expand|Select|Wrap|Line Numbers
  1. void sreplace(char newc, char oldc, char *s){
  2.  
  3. /* The test for the end of a string is a NULL character '\0'
  4.    this test  is likely to be true well after you have gone off the
  5.    end of the array s points to */    
  6.     while(char *s != EOF){
  7. /* This line sets the first character of s to oldc
  8.    this is not what you want to do at all */
  9.         *s== oldc;
  10. /* You test to see if the first character of s is equal to newc
  11.    that is not how you specified your function and since you have 
  12.    just set s to equal oldc it is guaranteed to be false */
  13.         if(*s == newc){
  14. /* You set oldc to the value of newc thus loosing the value of 
  15.    the character you are suposed to be replacing */
  16.             oldc=newc;
  17. /* You increment the pointer s (the dereference (*) is unrequired)
  18.    only if *s == newc which it never does, you need to increment s
  19.    everytime round the loop */
  20.             *s++;
  21.         }
  22.     }
  23. }
  24. /* Your return statement is outside the function, it should be
  25.    the last statement in the function (i.e. inside the last } )
  26. return;
  27.  
Sep 18 '06 #2

P: 61
/* This line sets the first character of s to oldc
this is not what you want to do at all */
*s== oldc;


This statement won't set anything. Rather this statement has no effect.
Sep 18 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
/* This line sets the first character of s to oldc
this is not what you want to do at all */
*s== oldc;


This statement won't set anything. Rather this statement has no effect.
Good point.

What pukur123 is refering to which I missed is the double = (==) it is a comparison not an assignment.
Sep 18 '06 #4

P: 11
//I made some changes and made comments. note the error messages at the
//bottom..... Thank you.

void sreplace(char newc, char oldc, char *s){

while(char *s != '\0'){
*s= oldc; //setting *s to the first character in the array
if(*s = newc){//
oldc=newc; //change oldc to newc
}
s++; //increment *s every time through the loop
}

return; //return inside the function ()
}

// i am getting the following errors when i compile:
// line3: error: syntax error before "char"
// line11: error: syntax error before "return"
//
Sep 18 '06 #5

Banfa
Expert Mod 5K+
P: 8,916
the char in the while statement is wrong it should be

while(*s != '\0'){

Do that and the loop is good, however you logic inside the loop is completely up the spout still

Expand|Select|Wrap|Line Numbers
  1.   *s = oldc;           //setting *s to the first character in the array  WHY?
  2.  
  3.   if(*s = newc){// And then this sets *s to newc, 
  4.                 // even if you meant == the logic is still wrong
  5.     oldc=newc;       //change oldc to newc Again WHY? What does this achieve?
  6.   }
  7.  
You do this and every character in the string will be set to newc. Try it on paper with simple data

sreplace('a', 'e', "red");

Actually step through the code at perform each operation on the data given.
Sep 18 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.