Veekay
Veekay

Reputation: 3

Copying selected character from string

My input string is

\\?\bac#dos&ven_bb&prod_open-v&rev_5001#1&7f6ac24&0&353020304346333030363338#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}

Required output is

bac\dos&ven_bb&prod_open-v&rev_5001\1&7f6ac24&0&353020304346333030363338_0

I have written a following code but is not working...need help is figuring out the problem. Forgive my ignorance :) Also let me know if there is any better and efficient way to do it.

The rule for the output string is

In the second string i am removing all the "\" and "?" .And where is see the "#" i replace it with "\". and the second string is only till you see the charater "{" but does not include "#" at the end of it.

THanks

int main() 
{
    char s[] = "\\?\bac#dos&ven_bb&prod_open-v&rev_5001#1&7f6ac24&0&353020304346333030363338#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}";
    char s1[] = {0};
    printf("OUtput string is : ");
    for(int i = 0; s[i] != '{'; i++)
    {
        if(s[i] != '\\' && s[i] != '?')
        {
            int j = 0;
            if(s[i] == '#')
            {
                s1[j] = '\\';
                continue;
            }

            s1[j] = s[i];
            j++;
        }

    }

    for(int i = 0; s1[i] != '\0'; i++)
    {
        cout<<s1[i];    
    }

    getch();
}

Upvotes: 0

Views: 180

Answers (3)

jrok
jrok

Reputation: 55395

Note the fixed scope of j. In your version you were always assigning to s1[0].

for(int i = 0, j = 0; s[i] != '{'; i++)
{
    if(s[i] != '\\' && s[i] != '?')
    {
        // int j = 0;
        if(s[i] == '#')
        {
            s1[j] = '\\';
        }
        else
        {
            s1[j] = s[i];
        }
        j++;
    } 
}

The other thing is to allocate enough space for the new string. Since you haven't specified the size char s1[] = {0}; declares an array of size 1. You need to do something like:

char s1[sizeof(s)] = { 0 }; // the size of the old array, since we don't know how long the new one will be

But since you tagged the Q C++, take advantage of of dynamically resizable std::string.

std::string s = ".......";
std::string s1;

for(int i = 0; s[i] != '{'; i++)
{
    if(s[i] != '\\' && s[i] != '?')
    {
        if(s[i] == '#')
            s1 += '\\';
        else
            s1 += s[i];
    } 
}

Upvotes: 1

AndersK
AndersK

Reputation: 36082

Your s1 buffer needs to be increased, as it stands now there is no room for the new string.

E.g.

char* s1 = calloc(strlen(s)+1,sizeof(char)); // same size should be enough, free(s1) later

the calloc ensures that it is \0 terminated, in your code you forgotten to add the \0 so the printout act erratically.

Upvotes: 0

mathematician1975
mathematician1975

Reputation: 21351

I would suggest looking into using the std::string::replace() function. There is plenty of online documentation on this. Take a look at some of the other functions that std::string has to offer as they might be of use too. If you are using c++, the use of std::string is usually preferable to tinkering with char arrays and indices.

Upvotes: 2

Related Questions