Results 1 to 4 of 4

Thread: Converting string to char

  1. #1
    Join Date
    Aug 2009
    Posts
    98

    Converting string to char

    I am having a small code created in C++. I want to convert string to char. I tried using explicit conversion but that isn't working. It still result in error messages. Is there any other way to convert string to char in C++?

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: Converting string to char

    Code:
    char convertStrToChar(const char *chr)
    {
        int i = 0;
        for(i = 0; *conv[i][1]; i++)
        {
                if(strcmp(conv[i][0], chr) == 0)
                {
                        return (char) conv[i][1];
                }
        }
        return '\0';
    }
    This code converts string to character.

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Converting string to char

    You can also something like:

    Code:
    string x;
    const char *p = "Hello";
    strcpy(x,p);
    cout<<x;

  4. #4
    Join Date
    May 2008
    Posts
    685

    Re: Converting string to char

    The below code can even convert the string "Hello" to char. The "substring" takes certain characters from the given string and converts it into char.

    Code:
    string str = "Hello";
    char c = (char)str.Substring(3,2);

Similar Threads

  1. Converting Boolean values to string in Java
    By 27lynx27 in forum Software Development
    Replies: 1
    Last Post: 13-02-2011, 03:40 AM
  2. How to convert string to char array in java?
    By Baazigar in forum Software Development
    Replies: 6
    Last Post: 10-01-2011, 06:36 PM
  3. Converting a string number into sequence of digits
    By KAIRU26 in forum Software Development
    Replies: 5
    Last Post: 13-03-2010, 04:46 PM
  4. Converting Char to Int in PHP
    By GlassFish in forum Software Development
    Replies: 4
    Last Post: 05-03-2010, 08:06 PM
  5. Converting Char value to ASCII number, C++
    By Gemstone29 in forum Software Development
    Replies: 2
    Last Post: 22-04-2009, 08:30 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,576,090.30749 seconds with 17 queries