-1

I am doing c programming after a while and I am stuck in a problem. I need to append characters in a pointer as follows.

#include<stdio.h>
#define l "a"
#define lt "<aa>"
#include<string.h>

main()
{
    char *st = (char*)malloc(sizeof(char));
    //char st[10];
    char *t = "abcdab";
    char *m ="y";

    while(*t)
    {
        if(*t == *l)
        {
            strcat(st, lt);
        }   

        else
        {
            //strcat(st, m); //strcat works in this case, But i need replacement.
            *st++ = *m; // How to make this line work to get desired output?
        }
        t++;
    }   
    printf("%s\n", st);
}   

As seen the code works by strcat. But I do not want to use strcat in else block. <aa>yyy<aa>y. Is my desired output which works with strcat in else block. But if I use "*st++ = *m", it prints only ''. So what do I need to add to make it print the expected output?

hshantanu
  • 424
  • 1
  • 8
  • 29
  • Research how `strcat` works and write your own custom function. – cadaniluk Nov 22 '15 at 10:36
  • @cad, I can do that. But I need to know what is going wrong with assignment I am doing in else block. It is printing only first occurrence instead of completely replacing the string. – hshantanu Nov 22 '15 at 10:38
  • @hshantanu you may like to read [Difference between `char *str` and `char str[]` and how both are stored in memory](http://stackoverflow.com/questions/15177420/what-does-sizeofarray-return/15177499#15177499) – Grijesh Chauhan Nov 22 '15 at 10:41
  • 1
    `*t = "abcdab"`, what is your expected output (`st`)? – artm Nov 22 '15 at 10:41
  • @artm "yyyy". Is my desired output which works with strcat in else block. But if I use "*st++ = *m", it prints only ''. So what do I need to add to make it print the expected output? – hshantanu Nov 22 '15 at 10:45
  • Because `*st++ = *m;` doesn't do `strcat()` does. `strcat()` copies chars until it finds a nul character whereas you assignment copies only one character. – P.P Nov 22 '15 at 10:46
  • 1
    What is the Input and what should be The output ? – Michi Nov 22 '15 at 10:49
  • @Michi "yyyy" should be the output. – hshantanu Nov 22 '15 at 10:53
  • @Blue Moon. Yes, so in order to make this work, do I need to add an extra while loop? – hshantanu Nov 22 '15 at 10:54
  • @hshantanu Yes. A loop and copy chars until you find terminating null char. – P.P Nov 22 '15 at 10:55

2 Answers2

1

Not sure why you wanted to do it that way. Anyway,

char *st = (char*)malloc(sizeof(char)); is not what you want. You should start with something like char st[100] = "";. More correctly, you would need to find out many a is there in the original string to find out the correct size for malloc of st.

I won't use #define for these l and lt, const would be better:

const char l = 'a';
const char lt[] = "<aa>";

if(*t == *l) would then be if(*t == l)

With those changes, *st++ = *m; becomes *(st + strlen(st)) = m; which should make the expected output.

artm
  • 17,291
  • 6
  • 38
  • 54
  • Thank you very much. It was a requirement I needed to do at work. This was sort of a practice code I was testing. – hshantanu Nov 22 '15 at 11:16
1

@Michi "<aa>yyy<aa>y" should be the output.

You could try something like this:

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

char *foo(const char *src, const char ch){
    char *dest = malloc(25 * sizeof(char*));
    int i=0,j=0;

    while(src[i] != '\0'){
        if(src[i] == ch){
            dest[j] = '<';
            j++;
            dest[j] = ch;
            j++;
            dest[j] = '>';
            j++;
        }
        if(src[i] != ch){
            dest[j] = 'y';
            j++;
        }
        i++;
    }
    dest[j] = '\0';
    return dest;
}

int main(void){
    char *t = "abcdab";
    char l = 'a';

    char *arr = foo(t,l);

    printf("%s\n",arr);
    free(arr);
    return 0;
}

Output:

<a>yyy<a>y

Of course you can do it without malloc too:

#include<stdio.h>

void foo(const char *src, const char ch){
    char dest[100];
    int i=0,j=0;

    while(src[i] != '\0'){
        if(src[i] == ch){
            dest[j] = '<';
            j++;
            dest[j] = ch;
            j++;
            dest[j] = '>';
            j++;
        }
        if(src[i] != ch){
            dest[j] = 'y';
            j++;
        }
        i++;
    }
    dest[j] = '\0';
    printf("%s\n",dest);
}

int main(void){
    char *t = "abcdab";
    char l = 'a';

    foo(t,l);
    return 0;
}
Michi
  • 5,175
  • 7
  • 33
  • 58