Fun and games with strings and pointers

I need help people:-


The program below is ok and working fine, however when i run it. It puts the pointer at the third letter of the string, showing the rest "LLO WORLD".


My question is."How can i extract the third letter?"


Thanks you kind people (code below) >:|




char *Stcpy(char *dst, char *src)

{

char *p = dst;


while ((*p++ = *src++) != '')

;

return dst;

}




void main()

{

char *source= "HELLO WORLD";

char dest[80];


Stcpy(dest,source);

printf("I have %s
",&dest[3]);


}


Comments

  • : I need help people:-


    : The program below is ok and working fine, however when i run it. It puts the pointer at the third letter of the string, showing the rest "LLO WORLD".


    : My question is."How can i extract the third letter?"


    : Thanks you kind people (code below) >:|


    :

    : char *Stcpy(char *dst, char *src)

    : {

    : char *p = dst;


    : while ((*p++ = *src++) != '')

    : ;

    : return dst;

    : }


    :

    : void main()

    : {

    : char *source= "HELLO WORLD";

    : char dest[80];


    : Stcpy(dest,source);

    : printf("I have %s
    ",&dest[3]);


    : }





  • : I need help people:-


    : The program below is ok and working fine, however when i run it. It puts the pointer at the third letter of the string, showing the rest "LLO WORLD".


    : My question is."How can i extract the third letter?"


    : Thanks you kind people (code below) >:|


    :

    : char *Stcpy(char *dst, char *src)

    : {

    : char *p = dst;


    : while ((*p++ = *src++) != '')

    : ;

    : return dst;

    : }


    :

    : void main()

    : {

    : char *source= "HELLO WORLD";

    : char dest[80];


    : Stcpy(dest,source);

    : printf("I have %s
    ",&dest[3]);


    : }


    First I'll ask: Why are you creating your own version of strcpy()? You can just use the version that comes with all compilers.


    Second, you reference single letters in strings with just the name of the string and the [x] where x is the character #-1.


    Ex:


    #include stdio.h

    #include string.h


    int main()

    {

    char *src="This is my string";

    char dest[20];


    strcpy(dest,src);


    printf("The third letter of dest is: %c
    ", dest[2]);


    return 0;


    }


    -Xotor-





  • : : I need help people:-


    : : The program below is ok and working fine, however when i run it. It puts the pointer at the third letter of the string, showing the rest "LLO WORLD".


    : : My question is."How can i extract the third letter?"


    : : Thanks you kind people (code below) >:|


    : :

    : : char *Stcpy(char *dst, char *src)

    : : {

    : : char *p = dst;


    : : while ((*p++ = *src++) != '')

    : : ;

    : : return dst;

    : : }


    : :

    : : void main()

    : : {

    : : char *source= "HELLO WORLD";

    : : char dest[80];


    : : Stcpy(dest,source);

    : : printf("I have %s
    ",&dest[3]);


    : : }


    Simply because you are printing a string from the 4th ([3]) character on.


    printf("I have %s
    ",&dest[3]);


    if you want to print just one char, use %c instead of %s







Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories