Thread: Printing part of a string

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    141

    Printing part of a string

    Hi,

    I've a string str as:

    HiHello

    I want to print only the Hello part i.e. from index 2 till the end like
    print(str[3...endofstr]

    How can I do that?

    Thanks,
    Angkar

  2. #2
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Code:
    for ( i = 2; i <= strlen( str ); i++ )
    {
    printf("%c", str[i] );
    }
    That should do the trick.

    p.s. sorry about the lack of tabs in my code

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    And what if I want to store the string str[3...endofstr] in some other variable?

  4. #4
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    Code:
    for( i=0, d=3; d < strlen( str ); i++,d++){
         str2[i]=str1[d];
    }
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  5. #5
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Or you could make:
    Code:
    {
       strncpy(str2,&str1[2],str2_size);
    }

  6. #6
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by bivhitscar
    Code:
    for ( i = 2; i <= strlen( str ); i++ )
    {
    printf("%c", str[i] );
    }
    That should do the trick.

    p.s. sorry about the lack of tabs in my code
    You can lose the for loop on that, if you're going to use printf():
    Code:
    printf("%s", str + 2);
    Of course, you should do bounds checking.
    Code:
    if(strlen(str) >= 2) printf("%s", str + 2);
    If you want to store it, another solution is:
    Code:
    sprintf(store_str, "%s", str + 2);
    Edit: Note that the "%s" isn't needed in any of those, but if you leave it out, someone can type something like "blah %d %d", and trick s/printf().
    Last edited by Cactus_Hugger; 04-23-2006 at 07:47 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  7. #7
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Or you could do something like this.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(int argc, char **argv) {
        if(argc !=2)
            exit(1);
    
        char temp[]="Hello";
    
        size_t len_temp = strlen(temp);
        size_t len_argv = strlen(argv[1]);
        size_t len_diff = len_argv - len_temp;
    
        if(len_diff != 2)
            exit(1);
    
        if( strstr(argv[1],temp) )
            printf("%s\n",temp);
    
        return 0;
    }
    $gcc -Wall match.c -o match
    $./match HiHello
    Hello
    $./match MeHello
    Hello
    $./match NoooHello
    $./match Hello
    $./match ribbit
    $./match Hell
    $

    Of course, the code is case sensitive.

  8. #8
    Registered User
    Join Date
    Feb 2006
    Location
    Great India
    Posts
    24
    Thanks to CDALTEN as his example taught me new thing i.e. size_t . It's really cool to use it instead of making new charecter type arrays.



    REALNAPSTER

  9. #9
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Quote Originally Posted by 00Sven
    Code:
    for( i=0, d=3; d < strlen( str ); i++,d++){
         str2[i]=str1[d];
    }
    ~Sven
    This will call strlen(str) for every iteration and so is much slower than if you only call strlen once.
    Code:
    int len = strlen(str);
    
    for( i=0, d=3; d < len; i++,d++){
         str2[i]=str1[d];
    }

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I think you're not understanding what they're doing. He's simply using a variable of type size_t to store the return value from the functions. Said functions return that type. It has nothing to do with "making new character type arrays" or not.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM