开发者

how to program in c to get the char and int separate in c? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 6 years ago.

开发者_如何学C Improve this question

Input: 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜