Hello, I have to write a program in which a string is converted into a group of characters which are then assigned an integer. So if the user enters the string "Hello World", the program would do: H = 1; e = 2; l = 3; o = 4; space = 5; W = 6; r = 7; d = 8.
Thank You
AFAIK, this can't be done. What you are trying to do would involve assigning a value (a number) to something which is also a literal value (a character).
Unfortunately for you, variable names are a completely different thing to a char value, and when you compile your program, the variable identifiers are lost.
As for a solution to your problem, you would need some sort of container (such as a std::pair) to hold both a char and an integer, and then some way to iterate through them (such as an array and looping).
int main()
{
char a[]="Hello World";
cout<<"\n";
int elem = 1;//used to count letters once
for(int i=0;i<11;i++)
{ if(i==3||i==7||i==9)continue;//jump the duplicates
if(i==5)cout<<"space = "<<elem<<",";
if (i==10)cout<<a[i]<<" = "<<elem<<".";//end with a period
elseif(i!=5)cout<<a[i]<<" = "<<elem<<",";//a[5] has already been dealt with
elem++;
}
return 0;
}
/*prints
H = 1,e = 2,l = 3,o = 4,space = 5,W = 6,r = 7,d = 8.
Process returned 0 (0x0) execution time : 0.109 s
Press any key to continue.
*/