c-string

can we change a c-string or extract integers from a c-string?
if yes then how?/
If you cannot work out how to change a variable, then you are not ready for C++.
I can't understand what you mean by changing a c-string.

To extract integers, you can use int atoi(char*) or long atol(char*) which are defined in stdlib.h. However, if you want to do it manually, follow this:
(where 'str' is your c-string and 'a' is the integer where you want to store your answer):

1
2
3
4
5
int a=0,i=0,s=1;	//s is to manage sign of the number
if(str[i]=='-'){s=-1;i++;}
else if(str[i]=='+')i++;
for(;str[i]>='0' && str[i]<='9';i++)a=10*a+int(str[i]-'0');
a*=s;
Last edited on
Topic archived. No new replies allowed.