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++?
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++?
This code converts string to character.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'; }
You can also something like:
Code:string x; const char *p = "Hello"; strcpy(x,p); cout<<x;
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);
Bookmarks