How do i convert a character array to string.

Discussion in 'C++' started by Venkat, Aug 19, 2005.

  1. Venkat

    Venkat Guest

    Hi All,

    Can someone tell me how do we convert a character array to string.
    I know the other way.

    For eg:-
    String strvar = "hello";
    printf("%s",strvar.c_str());

    regards,
    Venkat
     
    Venkat, Aug 19, 2005
    #1
    1. Advertising

  2. Venkat

    Srini Guest

    > Hi All,
    >
    > Can someone tell me how do we convert a character array to string.
    > I know the other way.
    >
    > For eg:-
    > String strvar = "hello";
    > printf("%s",strvar.c_str());
    >


    I hope you meant, string(lowercase 's'). And what are you doing in the
    first statement? Converting a array of chars to a string object.

    char arr[] = "Hello";
    std::string str(arr);

    > regards,
    > Venkat


    Srini
     
    Srini, Aug 19, 2005
    #2
    1. Advertising

  3. Venkat

    Default User Guest

    Venkat wrote:

    > Hi All,
    >
    > Can someone tell me how do we convert a character array to string.
    > I know the other way.
    >
    > For eg:-
    > String strvar = "hello";
    > printf("%s",strvar.c_str());


    I assume the "String" with the uppercase S is a typo, and you really
    mean std::string. std::string has both a constructor and an assignment
    operator that char* arguments.


    std::string s("hello");

    std::string s;

    s = "hello";



    You need to get a decent reference book that covers these things. This
    is basic stuff.




    Brian
     
    Default User, Aug 19, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brand Bogard

    8 bit character string to 16 bit character string

    Brand Bogard, May 25, 2006, in forum: C Programming
    Replies:
    8
    Views:
    742
    those who know me have no need of my name
    May 28, 2006
  2. Replies:
    3
    Views:
    499
    James Kanze
    Nov 18, 2007
  3. Tomás Ó hÉilidhe

    Convert native character string to ASCII array of integers

    Tomás Ó hÉilidhe, Mar 28, 2008, in forum: C Programming
    Replies:
    56
    Views:
    2,708
    CBFalconer
    Mar 31, 2008
  4. John Gregory
    Replies:
    0
    Views:
    297
    John Gregory
    Jul 5, 2009
  5. Bart Vandewoestyne
    Replies:
    8
    Views:
    742
    Bart Vandewoestyne
    Sep 25, 2012
Loading...

Share This Page