-3

As a learning technique, i'm suppose to make my own copy of the following string function in

    char * mystrcpy(char *a, char *b);
// string copy.  destroys a but not b.
// identical to strcpy in <string.h>
// running time O(mystrlen(b))

I've come with this

char * mystrcpy(char *a, char *b){
a = b;
return a;

}

since string a is a random chuck in memory I'm thinking to assign just to string b ... is my interpretation correct ?

sp_m
  • 2,647
  • 8
  • 38
  • 62
Thatdude1
  • 905
  • 5
  • 18
  • 31
  • 4
    You need to at least **try**. We're not here to just do your homework for you. Write some code which you think might work, and come back and ask specific questions if you get stuck. – Graham Borland Mar 20 '12 at 14:56
  • @GrahamBorland: Though your comment is generally true, the OP clearly specifies what he intends to do and asks specific questions regarding it. He is not asking for someone to make his homework for him. IMO it's a valid question :\ – amit Mar 20 '12 at 14:59
  • 3
    Please don't make this approach to your homework [your M.O.](http://stackoverflow.com/questions/9771624/introduction-to-arrays) around here. Try to answer the question yourself. Show us what you've tried. Even if it's completely and utterly wrong. – Bart Mar 20 '12 at 14:59
  • sorry, i didnt want the answer, the question i asked in the end basically i wanted it to be confirmed.. So i could attempt to try somethings ? :S – Thatdude1 Mar 20 '12 at 15:34

1 Answers1

2

accessing a specific char [in index i] in string is done using a[i], just like an array. [remember that in C, a string is actually an array of chars].

You should iterate the strings until you "see" a '\0' char - which indicate the end of string.

Yes, comparing to chars with operator< is comparing them by their ascii value - which is probably what you need.

amit
  • 175,853
  • 27
  • 231
  • 333
  • say i have `char *u = "bbcdef";` if i wanted to access 'b' i would do u[0] ? – Thatdude1 Mar 20 '12 at 15:45
  • @Beginnernato: Yes, you can get the first 'b' using `u[0]`. Note that because your string in here is a string literal you can only read `u[0]` and not change it. – amit Mar 20 '12 at 15:47
  • @Beginnernato `*b != '\0'` [in the loop's stop condition] checks only for the first element in the array, you are probably looking for `b[i] != '\0'`. also, in the first check of the loop: `(i > strlen(a)` You need `<=`, remember arrays [and strings] are starting from 0, not 1. – amit Mar 20 '12 at 16:23
  • any suggestion what i should do with `mystrcpy` , (Updated question) – Thatdude1 Mar 21 '12 at 03:26
  • @Beginnernato you should loop and copy each character using the same ideas from b to a – amit Mar 21 '12 at 05:46
  • Ohh i see, uhh for the last one strcat .. i got this http://ideone.com/wLeTP <-- it doesn't seem to work, how come ? :( – Thatdude1 Mar 21 '12 at 13:53