Ask your own question, for FREE!
Computer Science 10 Online
OpenStudy (anonymous):

Hey people :) Write a *C++* program to print the ASCII code for the corresponding *string*. Pleaseeeee Hellppp!! :')

OpenStudy (e.mccormick):

Funny thing about a string... if you convert it to a series of CHARs, those can be treated as INTs.

OpenStudy (anonymous):

i know that's what we're supposed to do, but i'm just confused about HOW to do it :( I mean, I got perfect output in C , but c++ is just so freaking confusing :P

OpenStudy (e.mccormick):

Here is an example I snagged: //std::string s char *a=new char[s.size()+1] a[s.size()]=0; memcpy(a,s.c_str(),s.size()); That is one way to do it.

OpenStudy (e.mccormick):

In that, you make a char array that is the proper size to hold the string, then copy the string to it.

OpenStudy (e.mccormick):

There is also strcopy.... or something like that. Let me find it.

OpenStudy (e.mccormick):

And this is a nice, clean method: http://stackoverflow.com/questions/347949/convert-stdstring-to-const-char-or-char Pretty similar to the first I grabed.

OpenStudy (rsmith6559):

Considering that C++ is a superset of C, a C solution is a C++ solution.

OpenStudy (e.mccormick):

As long as you include the right things.

OpenStudy (espex):

If all you must do is print the characters, you could simply create a for loop, iterate through the string and storing each index in an int and print to the screen. However, if you'd like to get creative you can always create a class to contain your char, the ascii value, and any other property you want.

OpenStudy (anonymous):

Hi Aaddiittii! Here is code snippet of your "ASCII value" question:-- #include<iostream.h> #include<conio.h> #include<stdio.h> void main() { clrscr(); int i,j,l; char ch,str[10]; cout<<"Hi Aaddiittii! First of all, enter the length of your string:"; cin>>l; cout<<"Enter the characters of the string sequentially & its respective ASCII code:\n"; for(i=1;i<=l;i++) { cin>>ch; cout<<(int)ch<<"-"; } getch(); }

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!