CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2000
    Posts
    196

    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!

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    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.

  3. #3
    Join Date
    Mar 2000
    Posts
    196

    Re: Replacing single character

    Yes, I mean a char. I thought that I could e.g. iterate through the string...

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Replacing single character

    Quote 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.

  5. #5
    Join Date
    Mar 2000
    Posts
    196

    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];
    			}
    		}
    }

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Replacing single character

    Quote 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

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    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.

  8. #8
    Join Date
    Feb 2003
    Posts
    377

    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.

  9. #9
    Join Date
    Mar 2000
    Posts
    196

    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
  •  





Click Here to Expand Forum to Full Width

Featured