how to copy a pointer string into a normal string which is of type "char"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prasad8619
    New Member
    • Jun 2008
    • 7

    how to copy a pointer string into a normal string which is of type "char"

    please help me out in this regard as this is urgent to me because i stuck in the middle of a program.......
    here I have a program like this......


    char name[]="rajesh";
    char *x=strchr(name, "j");
    int k =strlen(name);
    int i=x-name;
    int p=k-i;


    here I want to copy the string "jesh" into name[]..because i want 2 make it in loop....


    so i have written like this...

    name=&x[p];

    I know here the name[] is not of pointer type so it is not working...

    please help me....
    and I have one more doubt here. can we write strchr(name,'s' ); where name is of pointer type....
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    You can achive the same like this

    [code=c]
    int main()
    {
    char arr[] = "abcdef";
    printf("%s\n",a rr);
    sprintf(arr,"%s ",(arr+2));
    printf("%s\n",a rr);
    }

    [/code]

    You can use this logic with strchr to determine the start position

    Raghu

    Comment

    • ashitpro
      Recognized Expert Contributor
      • Aug 2007
      • 542

      #3
      First of all...
      I'd like to remind you that 'name' is character array...You can not change base address of any array.
      So "name=&x[p];" this statement is invalid...
      If name was character pointer then it would have worked.
      If you don't want it as character pointer..simply use for loop and copy the desired string.

      Regarding your second question...you can have character pointer as your first arg to strchr..provide d that it is null terminated....

      Comment

      • newb16
        Contributor
        • Jul 2008
        • 687

        #4
        Originally posted by prasad8619
        here I want to copy the string "jesh" into name[]..because i want 2 make it in loop....
        I don't get it. Do you want to use two loops?
        As x points to character within name, and x itself is a string, ewe can't use strcpy(name,x); Use memmove() instead as it's guaranteed to work with overlapped areas.


        and I have one more doubt here. can we write strchr(name,'s' ); where name is of pointer type....
        Yes.

        Comment

        • prasad8619
          New Member
          • Jun 2008
          • 7

          #5
          Iam very much thankful to all who helped me in solving this problem.....


          I did it using "for loop" only....


          Here I came to know one new thing that is while using pointer in "strchr" that should be terminated with "null" character...

          Thanks alot......the second one helped me alot in writing my program......

          Comment

          Working...