Convert string to char ?

This is a discussion on Convert string to char ? within the C++ Programming forums, part of the General Programming Boards category; How do you convert a string to a char ?...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    14

    Convert string to char ?

    How do you convert a string to a char ?

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(){
    	string mystring;
    	char mychar[50];
    
    	cout<<"Enter a string:";
    	getline(cin, mystring);
    	for(int i=0;i<50;i++)
    		mychar[i]=mystring[i];
    	cout<<mychar;
    	cin.get();
    	return(0);
    }
    I'm not sure why you wish to do this because a string is just a char pointer more or less and as you can see you can go through them just like char arrays(because they are char arrays).

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    -----
    char m_sArray[100];
    std::string m_sString = "freedom";

    if (m_sString.size() < 100)
    strcpy(m_sArray, m_sString.c_str(), m_sString.size());
    -----

    Kuphryn

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    14
    I am using Textou and it still doesnt want to work with the code above. I am using dev c++ too.

    heres my error:
    98 untitled1.cpp
    passing `char' to argument 4 of `TextOutA(HDC__ *, int, int, const CHAR *, int)' lacks a cast

  5. #5
    Unregistered
    Guest
    you can't convert a string to a char. A string is a group of char. You can convert each char in the string into a separate, independent char if you wish:

    char boo[3] = "pi";
    char first;
    char second;
    char third;

    boo is a string containg three char, two visible char (p and i) and one invsible (terminating null char).

    first = boo[0];
    second = boo[1];
    third = boo[2];

    This converts each char in boo to separate independent char types that can be used however you like, (you could have used the the left hand side representation any place you use the right hand representation too, but what the ----).

  6. #6
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    char boo[3] = "pi";
    char first;
    char second;
    char third;

    boo is a string containg three char, two visible char (p and i) and one invsible (terminating null char).
    Boo is a character array holding 4 elements, 0-3. In this instance, the second and third characters hold a null terminator. Elements zero and one hold "pi";

    Obviously boo should be declared as: char boo[2] = "pi";

  7. #7
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    Dual- Catfish wrote:
    Obviously boo should be declared as: char boo[2] = "pi";
    No, it shouldn't. [2] means that there are two(2) elements in the array. And now you are initializing three(3) elements there.

    Code:
    $ cat >boo.cpp
    #include <iostream>
    int main () {
     char boo[2] = "pi";
     std::cout<<boo<<std::endl;
     return 0;
    }
    $ g++ -o boo boo.cpp 
    boo.cpp: In function `int main ()':
    boo.cpp:3: initializer-string for array of chars is too long

  8. #8
    Unregistered
    Guest
    char boo[3] = "pi";

    results in a char array having three elements

    boo[0] == 'p'
    boo[1] == 'i'
    boo[2] == '\0'

    If you did this:

    char blah[2];
    blah[0] = 'p';
    blah[1] = 'i';

    and then tried this:

    cout << blah;

    your compiler should complain.

    if you did this however:
    char blah[3];
    blah[0] = 'p';
    blah[1] = 'i';
    blah[2] = '\0';
    cout << blah;

    everything would be fine. boo in initialized to a static string, whereas blah is a string assembled by assigning a single char at a time to a char array. Without the null char (aka, '\0' or just 0 which is zero, not capital oh) you don't have a string, you just have a char array.

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    1

    Smile Converting String to Char [] and Char ptr + copy char array to other chat []

    /* This program copies a character array into a given array */
    // this program uses 4 char ptrs
    // copy ptr to other char
    // convert a string to char [] and also set char ptr

    #include <iostream.h>

    #include <string>
    using namespace std;


    main( ){

    char strA[80] = "A quick brown fox jumps over a lazy dog. A test string \n";
    char strB[80];
    char strC[100];
    char strD[100];
    string hello;

    cout<< " Type the string \n ";
    getline( cin, hello);
    cout<< hello ;

    char *ptrA; /* a pointer to type character */
    char *ptrB; /* another pointer to type character */
    char *ptrC;
    char *ptrD;
    ptrA = strA; /* point ptrA at string A */
    ptrB = strB; /* point ptrB at string B */
    ptrC = strC;
    for(int i=0;i<100;i++)
    strD[i]=hello[i];

    ptrD = strD;

    cout<< " \n Now type to add in strC , it will be over written soon, note only one word is stored , try more \n ";
    cin >> strC;

    cout<< " \n Just have a look at strC[] , as it is stored " << strC << endl;

    while(*ptrA != '\0')
    {
    *ptrB++ = *ptrA++; // copying character by character
    }
    *ptrB = '\0';

    while(*ptrD != '\0') {
    *ptrC++ = *ptrD++; // copying character by character
    } *ptrC = '\0';



    cout << "\nString in strA = " << strA << endl; /* show strA on screen */
    cout << "\n String in strB = " << strB << endl; /* show strB on screen */
    cout << "\n String in strC = " << strC << endl; /* show strB on screen */
    cout << "\n String in strD = " << strD << endl; /* show strB on screen */

    system("pause");

    }

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why are you resurrecting a 7 year old thread???
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Not to mention you post code that will not even compile. Tsk, tsk.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 01:54 PM

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