How to get substring in C


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: How to get substring in C

  1. #1
    Barry Andrews Guest

    How to get substring in C


    Hi All,
    There seems to be no functions for getting a substring in C in the standard
    libraries. Can someone please provide an example of how I could do this?
    I want to be able to trim out the spaces at the end of a string.
    Many thanks for your help!!!


    Barry

  2. #2
    Brian Preston Guest

    Re: How to get substring in C


    strncpy is what you are looking for.

    Brian

    "Barry Andrews" <bandrews@level8.com> wrote:
    >
    >Hi All,
    > There seems to be no functions for getting a substring in C in the standard
    >libraries. Can someone please provide an example of how I could do this?
    >I want to be able to trim out the spaces at the end of a string.
    >Many thanks for your help!!!
    >
    >
    >Barry



  3. #3
    Barry Andrews Guest

    Re: How to get substring in C


    Are you refering to 'strcpy'? If so, I don't see how this will help me. All
    it does is copy one string to another. If there is a 'strncpy' function,
    I cannot find it in string.h. This must not be a standard C function. Does
    anyone have suggestions? Thanks!!

    Barry


    "Brian Preston" <brian81@yahoo.com> wrote:
    >
    >strncpy is what you are looking for.
    >
    >Brian
    >
    >"Barry Andrews" <bandrews@level8.com> wrote:
    >>
    >>Hi All,
    >> There seems to be no functions for getting a substring in C in the standard
    >>libraries. Can someone please provide an example of how I could do this?
    >>I want to be able to trim out the spaces at the end of a string.
    >>Many thanks for your help!!!
    >>
    >>
    >>Barry

    >



  4. #4
    Brian Preston Guest

    Re: How to get substring in C


    No I meant strncpy , I think this a standard function but I am not sure (I
    use VC++). Here is an example i copied from MSDN.

    /* STRNCPY.C */

    #include <string.h>
    #include <stdio.h>

    void main( void )
    {
    char string[100] = "Cats are nice usually";
    printf ( "Before: %s\n", string );
    strncpy( string, "Dogs", 4 );
    strncpy( string + 9, "mean", 4 );
    printf ( "After: %s\n", string );
    }


    Output

    Before: Cats are nice usually
    After: Dogs are mean usual


    Brian

    "Barry Andrews" <bandrews@level8.com> wrote:
    >
    >Are you refering to 'strcpy'? If so, I don't see how this will help me.

    All
    >it does is copy one string to another. If there is a 'strncpy' function,
    >I cannot find it in string.h. This must not be a standard C function. Does
    >anyone have suggestions? Thanks!!
    >
    >Barry
    >
    >
    >"Brian Preston" <brian81@yahoo.com> wrote:
    >>
    >>strncpy is what you are looking for.
    >>
    >>Brian
    >>
    >>"Barry Andrews" <bandrews@level8.com> wrote:
    >>>
    >>>Hi All,
    >>> There seems to be no functions for getting a substring in C in the

    standard
    >>>libraries. Can someone please provide an example of how I could do this?
    >>>I want to be able to trim out the spaces at the end of a string.
    >>>Many thanks for your help!!!
    >>>
    >>>
    >>>Barry

    >>

    >



  5. #5
    Barry Andrews Guest

    Re: How to get substring in C


    Ah yes, I did find this function, but here is my situation. I have a char[400],
    and I want to be able to trim the trailing blanks if there are any. So, for
    this string 'Hello ', will now be 'Hello'. I can see how
    strncpy could help me, but I am confused about how to find the first space
    so that I can cut off the rest of the string. This is easy to do in Java,
    but I am new to C programming. :0) Thanks again!!!


    Barry



    "Brian Preston" <brian81@yahoo.com> wrote:
    >
    >No I meant strncpy , I think this a standard function but I am not sure

    (I
    >use VC++). Here is an example i copied from MSDN.
    >
    >/* STRNCPY.C */
    >
    >#include <string.h>
    >#include <stdio.h>
    >
    >void main( void )
    >{
    > char string[100] = "Cats are nice usually";
    > printf ( "Before: %s\n", string );
    > strncpy( string, "Dogs", 4 );
    > strncpy( string + 9, "mean", 4 );
    > printf ( "After: %s\n", string );
    >}
    >
    >
    >Output
    >
    >Before: Cats are nice usually
    >After: Dogs are mean usual
    >
    >
    >Brian
    >
    >"Barry Andrews" <bandrews@level8.com> wrote:
    >>
    >>Are you refering to 'strcpy'? If so, I don't see how this will help me.

    >All
    >>it does is copy one string to another. If there is a 'strncpy' function,
    >>I cannot find it in string.h. This must not be a standard C function. Does
    >>anyone have suggestions? Thanks!!
    >>
    >>Barry
    >>
    >>
    >>"Brian Preston" <brian81@yahoo.com> wrote:
    >>>
    >>>strncpy is what you are looking for.
    >>>
    >>>Brian
    >>>
    >>>"Barry Andrews" <bandrews@level8.com> wrote:
    >>>>
    >>>>Hi All,
    >>>> There seems to be no functions for getting a substring in C in the

    >standard
    >>>>libraries. Can someone please provide an example of how I could do this?
    >>>>I want to be able to trim out the spaces at the end of a string.
    >>>>Many thanks for your help!!!
    >>>>
    >>>>
    >>>>Barry
    >>>

    >>

    >



  6. #6
    Brian Preston Guest

    Re: How to get substring in C


    This is how I would do it. 3 Diffrent ways.

    char src[100] = "0123456789 ";
    char dest[100];

    // keep the src intact
    int len = strlen(src);
    for(int i = 0; i < len; i++)
    {
    if(src[i] == ' ')
    {
    strncpy(dest, src, i);
    dest[i] = '\0';
    break;
    }

    }

    len = strlen(src);
    for(i = 0; i < len; i++)
    {
    if(src[i] == ' ')
    {
    // just append a null
    src[i] = '\0';
    break;
    }

    }

    // just using x to show strings end
    cout << dest << "x" << endl << src << "x" << endl;


    // STL makes this all very easy
    string s = "0123456789 ";
    s = s.substr( 0, s.find(' ') );

    cout << s << "x" << endl;

    Brian



    "Barry Andrews" <bandrews@level8.com> wrote:
    >
    >Ah yes, I did find this function, but here is my situation. I have a char[400],
    >and I want to be able to trim the trailing blanks if there are any. So,

    for
    >this string 'Hello ', will now be 'Hello'. I can see how
    >strncpy could help me, but I am confused about how to find the first space
    >so that I can cut off the rest of the string. This is easy to do in Java,
    >but I am new to C programming. :0) Thanks again!!!
    >
    >
    >Barry
    >
    >
    >
    >"Brian Preston" <brian81@yahoo.com> wrote:
    >>
    >>No I meant strncpy , I think this a standard function but I am not sure

    >(I
    >>use VC++). Here is an example i copied from MSDN.
    >>
    >>/* STRNCPY.C */
    >>
    >>#include <string.h>
    >>#include <stdio.h>
    >>
    >>void main( void )
    >>{
    >> char string[100] = "Cats are nice usually";
    >> printf ( "Before: %s\n", string );
    >> strncpy( string, "Dogs", 4 );
    >> strncpy( string + 9, "mean", 4 );
    >> printf ( "After: %s\n", string );
    >>}
    >>
    >>
    >>Output
    >>
    >>Before: Cats are nice usually
    >>After: Dogs are mean usual
    >>
    >>
    >>Brian
    >>
    >>"Barry Andrews" <bandrews@level8.com> wrote:
    >>>
    >>>Are you refering to 'strcpy'? If so, I don't see how this will help me.

    >>All
    >>>it does is copy one string to another. If there is a 'strncpy' function,
    >>>I cannot find it in string.h. This must not be a standard C function.

    Does
    >>>anyone have suggestions? Thanks!!
    >>>
    >>>Barry
    >>>
    >>>
    >>>"Brian Preston" <brian81@yahoo.com> wrote:
    >>>>
    >>>>strncpy is what you are looking for.
    >>>>
    >>>>Brian
    >>>>
    >>>>"Barry Andrews" <bandrews@level8.com> wrote:
    >>>>>
    >>>>>Hi All,
    >>>>> There seems to be no functions for getting a substring in C in the

    >>standard
    >>>>>libraries. Can someone please provide an example of how I could do this?
    >>>>>I want to be able to trim out the spaces at the end of a string.
    >>>>>Many thanks for your help!!!
    >>>>>
    >>>>>
    >>>>>Barry
    >>>>
    >>>

    >>

    >



  7. #7
    Barry Andrews Guest

    Re: How to get substring in C


    Cool! This is great! I really appreciate your help!
    If you don't mind one more question, I would like to ask another. In Java,
    there are very nice Javadocs that give lots of information about the classes
    and methods. Is there anything like that in C? It is difficult (or impossible
    for me) to just look at a header file and figure out what a function does.
    Does C have anything that describes the parameters and also describes what
    the function does? Thanks again!


    Barry



    "Brian Preston" <brian81@yahoo.com> wrote:
    >
    >This is how I would do it. 3 Diffrent ways.
    >
    > char src[100] = "0123456789 ";
    > char dest[100];
    >
    > // keep the src intact
    > int len = strlen(src);
    > for(int i = 0; i < len; i++)
    > {
    > if(src[i] == ' ')
    > {
    > strncpy(dest, src, i);
    > dest[i] = '\0';
    > break;
    > }
    >
    > }
    >
    > len = strlen(src);
    > for(i = 0; i < len; i++)
    > {
    > if(src[i] == ' ')
    > {
    > // just append a null
    > src[i] = '\0';
    > break;
    > }
    >
    > }
    >
    > // just using x to show strings end
    > cout << dest << "x" << endl << src << "x" << endl;
    >
    >
    > // STL makes this all very easy
    > string s = "0123456789 ";
    > s = s.substr( 0, s.find(' ') );
    >
    > cout << s << "x" << endl;
    >
    >Brian
    >
    >
    >
    >"Barry Andrews" <bandrews@level8.com> wrote:
    >>
    >>Ah yes, I did find this function, but here is my situation. I have a char[400],
    >>and I want to be able to trim the trailing blanks if there are any. So,

    >for
    >>this string 'Hello ', will now be 'Hello'. I can see how
    >>strncpy could help me, but I am confused about how to find the first space
    >>so that I can cut off the rest of the string. This is easy to do in Java,
    >>but I am new to C programming. :0) Thanks again!!!
    >>
    >>
    >>Barry
    >>
    >>
    >>
    >>"Brian Preston" <brian81@yahoo.com> wrote:
    >>>
    >>>No I meant strncpy , I think this a standard function but I am not sure

    >>(I
    >>>use VC++). Here is an example i copied from MSDN.
    >>>
    >>>/* STRNCPY.C */
    >>>
    >>>#include <string.h>
    >>>#include <stdio.h>
    >>>
    >>>void main( void )
    >>>{
    >>> char string[100] = "Cats are nice usually";
    >>> printf ( "Before: %s\n", string );
    >>> strncpy( string, "Dogs", 4 );
    >>> strncpy( string + 9, "mean", 4 );
    >>> printf ( "After: %s\n", string );
    >>>}
    >>>
    >>>
    >>>Output
    >>>
    >>>Before: Cats are nice usually
    >>>After: Dogs are mean usual
    >>>
    >>>
    >>>Brian
    >>>
    >>>"Barry Andrews" <bandrews@level8.com> wrote:
    >>>>
    >>>>Are you refering to 'strcpy'? If so, I don't see how this will help me.
    >>>All
    >>>>it does is copy one string to another. If there is a 'strncpy' function,
    >>>>I cannot find it in string.h. This must not be a standard C function.

    >Does
    >>>>anyone have suggestions? Thanks!!
    >>>>
    >>>>Barry
    >>>>
    >>>>
    >>>>"Brian Preston" <brian81@yahoo.com> wrote:
    >>>>>
    >>>>>strncpy is what you are looking for.
    >>>>>
    >>>>>Brian
    >>>>>
    >>>>>"Barry Andrews" <bandrews@level8.com> wrote:
    >>>>>>
    >>>>>>Hi All,
    >>>>>> There seems to be no functions for getting a substring in C in the
    >>>standard
    >>>>>>libraries. Can someone please provide an example of how I could do

    this?
    >>>>>>I want to be able to trim out the spaces at the end of a string.
    >>>>>>Many thanks for your help!!!
    >>>>>>
    >>>>>>
    >>>>>>Barry
    >>>>>
    >>>>
    >>>

    >>

    >



  8. #8
    Brian Preston Guest

    Re: How to get substring in C


    I use the MSDN library from microsoft. It has docs on all the C Runtime functions
    and also microsoft specific stuff. You can get a cd which I recommend or
    they have it online. Using the online version is a pain in my opinon.

    http://msdn.microsoft.com/library/default.asp

    http://msdn.microsoft.com/library/de...ML/clanghm.asp



    Brian

    "Barry Andrews" <bandrews@level8.com> wrote:
    >
    >Cool! This is great! I really appreciate your help!
    >If you don't mind one more question, I would like to ask another. In Java,
    >there are very nice Javadocs that give lots of information about the classes
    >and methods. Is there anything like that in C? It is difficult (or impossible
    >for me) to just look at a header file and figure out what a function does.
    >Does C have anything that describes the parameters and also describes what
    >the function does? Thanks again!
    >
    >
    >Barry
    >
    >
    >
    >"Brian Preston" <brian81@yahoo.com> wrote:
    >>
    >>This is how I would do it. 3 Diffrent ways.
    >>
    >> char src[100] = "0123456789 ";
    >> char dest[100];
    >>
    >> // keep the src intact
    >> int len = strlen(src);
    >> for(int i = 0; i < len; i++)
    >> {
    >> if(src[i] == ' ')
    >> {
    >> strncpy(dest, src, i);
    >> dest[i] = '\0';
    >> break;
    >> }
    >>
    >> }
    >>
    >> len = strlen(src);
    >> for(i = 0; i < len; i++)
    >> {
    >> if(src[i] == ' ')
    >> {
    >> // just append a null
    >> src[i] = '\0';
    >> break;
    >> }
    >>
    >> }
    >>
    >> // just using x to show strings end
    >> cout << dest << "x" << endl << src << "x" << endl;
    >>
    >>
    >> // STL makes this all very easy
    >> string s = "0123456789 ";
    >> s = s.substr( 0, s.find(' ') );
    >>
    >> cout << s << "x" << endl;
    >>
    >>Brian
    >>
    >>
    >>
    >>"Barry Andrews" <bandrews@level8.com> wrote:
    >>>
    >>>Ah yes, I did find this function, but here is my situation. I have a char[400],
    >>>and I want to be able to trim the trailing blanks if there are any. So,

    >>for
    >>>this string 'Hello ', will now be 'Hello'. I can see how
    >>>strncpy could help me, but I am confused about how to find the first space
    >>>so that I can cut off the rest of the string. This is easy to do in Java,
    >>>but I am new to C programming. :0) Thanks again!!!
    >>>
    >>>
    >>>Barry
    >>>
    >>>
    >>>
    >>>"Brian Preston" <brian81@yahoo.com> wrote:
    >>>>
    >>>>No I meant strncpy , I think this a standard function but I am not sure
    >>>(I
    >>>>use VC++). Here is an example i copied from MSDN.
    >>>>
    >>>>/* STRNCPY.C */
    >>>>
    >>>>#include <string.h>
    >>>>#include <stdio.h>
    >>>>
    >>>>void main( void )
    >>>>{
    >>>> char string[100] = "Cats are nice usually";
    >>>> printf ( "Before: %s\n", string );
    >>>> strncpy( string, "Dogs", 4 );
    >>>> strncpy( string + 9, "mean", 4 );
    >>>> printf ( "After: %s\n", string );
    >>>>}
    >>>>
    >>>>
    >>>>Output
    >>>>
    >>>>Before: Cats are nice usually
    >>>>After: Dogs are mean usual
    >>>>
    >>>>
    >>>>Brian
    >>>>
    >>>>"Barry Andrews" <bandrews@level8.com> wrote:
    >>>>>
    >>>>>Are you refering to 'strcpy'? If so, I don't see how this will help

    me.
    >>>>All
    >>>>>it does is copy one string to another. If there is a 'strncpy' function,
    >>>>>I cannot find it in string.h. This must not be a standard C function.

    >>Does
    >>>>>anyone have suggestions? Thanks!!
    >>>>>
    >>>>>Barry
    >>>>>
    >>>>>
    >>>>>"Brian Preston" <brian81@yahoo.com> wrote:
    >>>>>>
    >>>>>>strncpy is what you are looking for.
    >>>>>>
    >>>>>>Brian
    >>>>>>
    >>>>>>"Barry Andrews" <bandrews@level8.com> wrote:
    >>>>>>>
    >>>>>>>Hi All,
    >>>>>>> There seems to be no functions for getting a substring in C in

    the
    >>>>standard
    >>>>>>>libraries. Can someone please provide an example of how I could do

    >this?
    >>>>>>>I want to be able to trim out the spaces at the end of a string.
    >>>>>>>Many thanks for your help!!!
    >>>>>>>
    >>>>>>>
    >>>>>>>Barry
    >>>>>>
    >>>>>
    >>>>
    >>>

    >>

    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center