Frage

I am trying to write a really simple program, but I can't find the problem here. Tried different methods, this is what I tried now:

   #include <stdio.h>
void copyStr(char *p, char *h){

   int i=0,j=0;
   int length=0;
   length=strlen(p); int l=length;
   for (i=0; i<length; i++){
       h[i]=p[l-1];
       l--;
   }
   char *temp=&h[0];
   for (i=0; i<length; i++){
       printf("%c",temp[i]);
   }


}
main(){

    char p[]="abcde";
    char h [sizeof(p)];
    copyStr(p,h);
}

When I copy these strings, the first letter doesn't seem to be copied.

My assignment is actually bigger, trying to copy the strings in REVERSE, but I believe finding out what went wrong here will help me succeed.

Any help is appriciated.

EDIT: solved, code is now working.

War es hilfreich?

Lösung

Here is the C code to reverse a string,

void reverse(char *string) 
{
   int length, c;
   char *begin, *end, temp;

   length = strlen(string);

   begin = string;
   end = string;

   for ( c = 0 ; c < ( length - 1 ) ; c++ )
      end++;

   for ( c = 0 ; c < length/2 ; c++ ) 
   {        
      temp = *end;
      *end = *begin;
      *begin = temp;

      begin++;
      end--;
   }
}

Andere Tipps

Many problems here...

  1. Pass p and h as arguments, don't use &p. The variable p is already a pointer to the array of characters. & makes it a pointer-to-pointer.

  2. You're copying backwards in your loop, assigning h to p.

  3. Your print loop is broken: the termination condition should be *p not p. Also p has already been advanced to the end of the string by your copy code.

  4. swap() is a misleading name. It's not swapping two strings. It's copying one to the other. Even later when you add reversing, it's still reversing, not swapping.

  5. You should declare the source string argument as const. This would have detected problem 2 above.

After the iteration of:

while(*p++=*h++){
    ;
}

Finishes, p will be pointing to the terminating \0 character of the copy of the string. After that in this cycle:

while (p){
    printf("%c",p[i++]);
}

p will be pointing to that element. You will be printing character after it(i.e. out of the string) and also this cycle will only terminate when p becomes 0(which will happen after integer overflow). Actually the program will crash way before that. The hacky approach you use for copying strings may be very fancy, but not very useful in almost all cases. I suggest you simply use standard library's functions like strcpy.

Use this to implement the strcpy

char * strcpy(char *strDest, const char *strSrc)
{
    assert(strDest!=NULL && strSrc!=NULL);
    char *temp = strDest;
    while(*strDest++ = *strSrc++); // or while((*strDest++=*strSrc++) != '\0');
    return temp;
}

This is the code that works:

#include <stdio.h>
void copyStr(char *p, char *h){

   int i=0,j=0;
   int length=0;
   length=strlen(p); int l=length;
   for (i=0; i<length; i++){
       h[i]=p[l-1];
       l--;
   }
   char *temp=&h[0];
   for (i=0; i<length; i++){
       printf("%c",temp[i]);
   }


}
main(){

    char p[]="abcde";
    char h [sizeof(p)];
    copyStr(p,h);
}

Your feedback is very important, thank you.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top