•
•
•
•
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
![]() |
•
•
Join Date: Aug 2008
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
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.
> 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
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.
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.
Tested what?
If you made changes, and it doesn't work, then post your updated code.
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.
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.
•
•
Join Date: Aug 2008
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
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));
}
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.
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.
char *replace(char string[100])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
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
Similar Threads
- Replacing characters in a foldername (Shell Scripting)
- Replacing 2 characters with with preg_replace (PHP)
- replace characters (C++)
- Replacing a field separator during a read. (Perl)
- problems with function pointers (C)
- replacing unprintable characters (C)
- Replacing a large amount of text (Shell Scripting)
Other Threads in the C Forum
- Previous Thread: need help with a calculator also diffrent reason
- Next Thread: Help with variables and filenames



Linear Mode