take a piace of a char

This is a discussion on take a piace of a char within the C Programming forums, part of the General Programming Boards category; How is it possible to take part of a char and place it in a other char like Code: #include ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    29

    take a piace of a char

    How is it possible to take part of a char and place it in a other char like

    Code:
    #include <stdio.h>
    
    #include <unistd.h>
    
    int main()
    {
    
    char cpr1[20] = "This is a test";
    char cpr2[5];
    int i;
    for(i=0; i<5; i++)
    {
    cpr2[i] = cpr1[i]
    }
    }
    this code is ofcause not working...
    i want the word "This" to be in cpr2. how can i do that??

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    your code is allmost working. You just forgot about the terminating '\0'.
    Code:
    #include <stdio.h>
    
    int main(){
    
       char cpr1[20] = "This is a test";
       char cpr2[5];
       int i;
       for(i=0; i<4; i++) {
           cpr2[i] = cpr1[i]
       }
       cpr2[4]=0;
       return 0;   // required
    }
    you could also use strncpy().
    Kurt

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    32
    And the semicolon:

    cpr2[i] = cpr1[i];


  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    hmm its not working..
    i changed the code and put a printf in it

    Code:
    #include <stdio.h>
    
    #include <unistd.h>
    
    int main(){
    
       char cpr1[20] = "This is a test";
       char cpr2[5];
       int i;
       for(i=0; i<4; i++) {
           cpr2[i] = cpr1[i];
       };
       cpr2[4]=0;
    
    	printf("cpr2 is %c\n", cpr2);
       return 0;   // required
    }

    how can i use strncpy() if i kind of want to add an ekstra letter

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    it's not working because your formatstring is wrong. use that instead
    Code:
    printf("cpr2 is %s\n", cpr2);
    > how can i use strncpy() if i kind of want to add an ekstra letter
    I don't understand that.

    Kurt

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    32
    The for loop must not end with a semicolon if you're using braces:

    Code:
    for(i=0; i<4; i++) {
        cpr2[i] = cpr1[i];
    }

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    oh yeah %s.. thats right.. thanks

  8. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by tretton
    The for loop must not end with a semicolon if you're using braces:
    It's useless but legal.
    Kurt

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    but can i ask u one thing

    y do u write cpr2[4]=0;

    and what does the "4" mean

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    32
    Oh, yeah. But in this case, the semicolon is just an empty statement that has nothing to do with the loop, right? Like:

    Code:
    for (blabla) {}
    
    ;
    Not that it matters or anything.

  11. #11
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    Hi

    "how can i use strncpy() if i kind of want to add an ekstra letter"

    If you want to append your string use strcat() or strencat(). It seems that you are using a UNIX based system (cool ), so "man strcat" will give you enough information.

  12. #12
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    >and what does the "4" mean
    it means 4 the index of the 5th character.
    I could have written
    Code:
    i = 4;
    cpr2[i] = '\0';
    same difference.
    Kurt

  13. #13
    Registered User
    Join Date
    Dec 2005
    Posts
    32
    The cpr2[4]=0; assigns the nul character (string terminator) to the fifth (0, 1, 2, 3, 4) character in the string. You need the terminator since there is no other way of telling where the string ends.

  14. #14
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by tretton
    Oh, yeah. But in this case, the semicolon is just an empty statement that has nothing to do with the loop, right? Like:

    Code:
    for (blabla) {}
    
    ;
    honestly I don't know. Propably is an empty statement.
    Kurt

  15. #15
    Sys.os_type="Unix";;
    Join Date
    Aug 2005
    Posts
    52
    You can also use sprintf

    Code:
    sprintf(cpr2, "%.*s", 5, cpr1);
    Or strncpy or multiple different things depend on the circumstances.
    Last edited by Sysop_fb; 02-01-2006 at 02:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 01:06 PM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 10:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21