-1
void SpacePlug(char *StringPtr, char Ch)
{
    int i = 0;
    while (*(StringPtr + i)!= '\0')
    {
        if (*(StringPtr + i)== ' ')
        {
            *(StringPtr + i ) = '^^';
            printf("%c",*(StringPtr + i));
        }
        i++;
    }
}
int main()
{
    char a[]= "Alton Tait";
    SpacePlug(a,);
}

Function is to replace each space in the string with the character .In main, use SpacePlug i want to replace the space between alton tait with ^^ so it should be alton^^tait thats what i come up with i cant i would like to know where i went wrong. thank you

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
A.Tait
  • 1
  • 2

2 Answers2

0

'^^' is not a character. It is a multi-character constant, which is not portable.

Your code is good for a single-character replacements, i.e.

SpacePlug(a, '^');

You also need to move printing out of the if:

int i = 0;
while (*(StringPtr + i)!= '\0')
{
    if (*(StringPtr + i)== ' ')
    {
        *(StringPtr + i ) = '^';
    }
    printf("%c", *(StringPtr + i));
    i++;
}

Demo.

To make replacements for multiple characters you need an entirely different approach:

  • Pass char* for the replacement
  • Make sure the string has enough space to expand for the extra characters
  • Do the replacement in two passes
  • Compute the total length after replacement in the first pass
  • Starting from the back, perform replacements as you go, using the same space.
Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Sorry about the double character '^^' i only need it to replace with one thank for your help really appreciate . – A.Tait Apr 16 '17 at 18:56
  • wheneve i run it, its only printing out ^ its not printing out Alton^tait. – A.Tait Apr 16 '17 at 19:13
0

This is the output i get when i try to compile your code using gcc:

In function 'SpacePlug':
8:33: warning: multi-character character constant [-Wmultichar]
             *(StringPtr + i ) = '^^';
                                 ^
8:33: warning: overflow in implicit constant conversion [-Woverflow]
 In function 'main':
17:17: error: expected expression before ')' token
     SpacePlug(a,);

you should hace included the error report in the question, so it's easier to see what's going on.

You've got a few problems on your code:

  • "^^" is not a character, but a string with 2 characters. '^' is a character. That's the reason for the "multi-charater" error

  • You're not using "Ch" inside SpacePlug. The replacing character is hardcoded. I'ts always '^^', which doesn't exist.

  • The function is not properly called in main. It's missing a parameter.

Now for the solution. What i understood is that "SpacePlug" tries to find all spaces inside a string, the first parameter, and replace them with a character, which is the second parameter. The following code will work just fine for that:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void SpacePlug(char *StringPtr, char Ch, char *newString)
{   
    for (int i = 0; i < strlen(StringPtr); i++)
    {
        if (StringPtr[i] == ' ')
        {
            newString[i] = Ch;
        }
        else
        {
            newString[i] = StringPtr[i];
        }
    }
}
int main()
{
    char *a = "Alton Tait";
    char replace = '^';
    char *newString = (char *)malloc(strlen(a) + 1); // the +1 is for the null terminator
    SpacePlug(a, replace, newString);
    printf("%s\n", newString);

    free(newString);
}

Cheers.

neoland
  • 36
  • 4
  • Thanks for your help but the code is not compiling. – A.Tait Apr 16 '17 at 19:01
  • This code fails to NULL terminate `newString`. The allocation also needs to be larger to account for the NULL. – Blastfurnace Apr 16 '17 at 19:03
  • There, i edited it so a new string is taken as a parameter instead of creating it inside function. I initialize the memory space and then destroy it before leaving. Hope it compiles this time. – neoland Apr 16 '17 at 19:24