I am trying to sort integers and strings from an input string.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
int main(){
char x[10];
int y;
printf("string: ");
scanf("%s",x);
y=atoi(x);
printf("\n %d", y);
getchar();
getchar(); }
suppose the input is 123abc1
using atoi i could extract 123 from the input string, my question now is how do i extract abc1?
I want to store abc1 on a separate character variable.
input: 123abc1
output: x = 123, some char variable = abc1
I appreciate any help.
If you wish to use the C programming language concepts, then consider using
strtolintead ofatoi. It will let you know what character did it stop at:Also, never use
%sin ascanf, always specify the buffer size (minus one, since %s will add a ‘\0’ after storing your input)test: https://ideone.com/uCop8
In C++, if that tag was not a mistake, there are simpler approaches, such as stream I/O.
For example,
test: https://ideone.com/dWYPx