|
-
May 6th, 2008, 02:23 PM
#1
Replacing single character
Hello
I'm searching for a simple way to replace all occurences of a single character with another single character in a string (e.g. replacing 'a' by 'b').
I was already looking on the internet and in string.h but it seems that there is no replace-function in C, only in C++... Is there someone who can help me solving that issue in C?
Thank you very much!
-
May 6th, 2008, 02:29 PM
#2
Re: Replacing single character
What kind of string? Since you're asking about C, I assume you mean a char array.
You can just examine each character in the array and replace it when appropriate.
-
May 6th, 2008, 02:34 PM
#3
Re: Replacing single character
Yes, I mean a char. I thought that I could e.g. iterate through the string...
-
May 6th, 2008, 02:41 PM
#4
Re: Replacing single character
 Originally Posted by frei
Yes, I mean a char. I thought that I could e.g. iterate through the string...
You can. That's what I said to do.
string is a C++ data type. You can get better help if you use the correct terminology for what you're doing.
-
May 6th, 2008, 02:50 PM
#5
Re: Replacing single character
O.k. what I did now is a iteration through the string as follows. Works perfect for me. Do you think that is "good" code for solving that issue?
Code:
void replace (char string[], char from[], char to[])
{
int start, i1, i2;
for(start = 0; string[start] != '\0'; start++)
{
i1 = 0;
i2 = start;
while(from[i1] != '\0')
{
if(from[i1] != string[i2])
break;
i1++;
i2++;
}
if(from[i1] == '\0')
{
for(i1 = 0; to[i1] != '\0'; i1++)
string[start++] = to[i1];
}
}
}
-
May 6th, 2008, 02:59 PM
#6
Re: Replacing single character
 Originally Posted by frei
O.k. what I did now is a iteration through the string as follows. Works perfect for me. Do you think that is "good" code for solving that issue?
This code looks nothing like any solution to the original issue you mentioned.
You stated that you want to replace a character with another character. Here is how the loop should look:
Code:
#include <string.h>
void replaceChar(char *input, char origChar, char repChar)
{
int nLen = strlen(input);
int curChar;
for (curChar = 0; curChar < nLen; ++curChar)
{
if ( input[curChar] == origChar )
input[curChar] = repChar;
}
}
I don't know what that code is that you wrote, but it does nothing that you stated you really wanted to do (if it does, it is very convoluted).
Regards,
Paul McKenzie
-
May 6th, 2008, 03:11 PM
#7
Re: Replacing single character
I can't easily tell what you're trying to do either, but agree that it doesn't seem to match the problem description. And you really need to stop saying "string" when you don't mean it.
-
May 6th, 2008, 04:51 PM
#8
Re: Replacing single character
"string" is a common computing term that describes a sequence of characters (or in some cases other things). There's nothing wrong with using it in this context. A null-terminated character array is a string in C (and C++).
Obviously there can be confusion when a forum handles C and C++ questions and C++ has a standard class called "string", but the onus is on the person referring to the string class to identify it as such, not the person using the generic term.
In my opinion, of course.
-
May 7th, 2008, 04:50 AM
#9
Re: Replacing single character
@Paul McKenzie
You're code is really better and works much faster Thank you!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|