User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 391,927 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,670 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 234 | Replies: 8 | Solved
Reply
Join Date: Aug 2008
Posts: 5
Reputation: jess29 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jess29 jess29 is offline Offline
Newbie Poster

need help replacing characters

  #1  
14 Days Ago
I just started teaching myself c with a book called practical c programming, and i can't figure out one of the exercises. I'm supposed to write a function that scans a character array for the character - and replace it with _. I tried doing it, but i don't think i fully understand how to pass arrays between functions or whatever. Here's what i got, please help me.

char replace(char string[100])
{
 int i; //increment
 
 for (i=0; string != '\0'; i++)
  {
   if (string[i] == '-')
     string[i] = '_';
 }
 return(string[i]);
}

int main()
{
 char line[100];

 printf("Enter a string: ");
 fgets(line, sizeof(line), stdin);

 printf("New string: %s", replace(line));
} 
Last edited by jess29 : 14 Days Ago at 10:12 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,261
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 368
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: need help replacing characters

  #2  
14 Days Ago
> string != '\0'
Compare with string[i] != '\0'

Also, make the function return a char*, and return string.
That is, if you want to print the result with %s
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Aug 2008
Posts: 5
Reputation: jess29 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jess29 jess29 is offline Offline
Newbie Poster

Re: need help replacing characters

  #3  
14 Days Ago
i tested it and it just said memory fault
Reply With Quote  
Join Date: Dec 2005
Posts: 3,261
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 368
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: need help replacing characters

  #4  
14 Days Ago
Tested what?
If you made changes, and it doesn't work, then post your updated code.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Aug 2008
Posts: 5
Reputation: jess29 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jess29 jess29 is offline Offline
Newbie Poster

Re: need help replacing characters

  #5  
14 Days Ago
char replace(char string[100])
{
 int i; //increment

 for (i=0; string[i] != '\0'; i++)
  {
   if (string[i] == '-')
     string[i] = '_';
 }
 return(*string);
}

int main()
{
 char line[100];

 printf("Enter a string: ");
 fgets(line, sizeof(line), stdin);

 printf("New string: %s", replace(line));
}
Reply With Quote  
Join Date: Dec 2005
Posts: 3,261
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 368
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: need help replacing characters

  #6  
14 Days Ago
char *replace(char string[100])
and
return(string);
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Aug 2008
Posts: 5
Reputation: jess29 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jess29 jess29 is offline Offline
Newbie Poster

Re: need help replacing characters

  #7  
14 Days Ago
thank you
Reply With Quote  
Join Date: Dec 2006
Posts: 1,284
Reputation: Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all 
Rep Power: 9
Solved Threads: 88
Aia's Avatar
Aia Aia is offline Offline
Nearly a Posting Virtuoso

Re: need help replacing characters

  #8  
14 Days Ago
char *replace(char string[100])
And now something extra. You don't need to care how long the string passed as an argument is.
You can write it like char *replace ( char string[] ) or even like char *replace( char *string ).
Last edited by Aia : 14 Days Ago at 5:54 pm.
"No man's life, liberty, or property is safe while the legislature is in session." ~ Mark Twain
Reply With Quote  
Join Date: Aug 2008
Posts: 5
Reputation: jess29 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jess29 jess29 is offline Offline
Newbie Poster

Re: need help replacing characters

  #9  
13 Days Ago
Well i haven't gotten to pointers yet, i started reading about it in the first book i tried, but they're more towards the end of this one.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 8:24 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC