how to program in c to get the char and int separate in c? [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
开发者_如何学C Improve this questionInput: AB5, BC2, CD3, BE4
When we input i/p to a program as above. we need to get each string separated by "," and also need to separate string to char by char like getting 'A' 'B' '5' .. how to do this efficiently in c???
I have implemented like storing whole string in an character array and then for loop processing each index of char array to get the char by char by that string..
char a[1000] = "AB5,BC2,CD3";
len = strlen(a);
for (int i=0;i<len;i++)
printf("%c",a[i]);
But is there any other efficient way of doing the above?
int c;
while ((c = getchar()) != EOF) {
if (c == ',')
putchar('\n');
else if (isalnum(c))
putchar(c);
}
does the same as your program, but without the array, translating ,
to newline and swallowing whitespace.
I'd do this in two passes. First, I'd use strtok()
to separate the data on the commas (delimiter characters ','
and ' '
).
Once you've done that, you can simple separate each character of each token.
精彩评论