Skip to content

Instantly share code, notes, and snippets.

@otaks
Last active May 1, 2016 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otaks/ab1500c32f99a58c3bfce10be2ed5b85 to your computer and use it in GitHub Desktop.
Save otaks/ab1500c32f99a58c3bfce10be2ed5b85 to your computer and use it in GitHub Desktop.
sscanf
#include <stdio.h>
#include <string.h>
int main() {
char tmp[3][10];
char str[10] = "kor,,st";
char tm[10] = { 0 };
strcpy(tm, str);
memset(str, 0x00, sizeof(str));
int j = 0;
//半角スペースを挿入する
for (int i = 0; i<strlen(tm); i++) {
if (tm[i] == ',') {
str[j] = ' ';
j++;
str[j] = tm[i];
j++;
}
else {
str[j] = tm[i];
j++;
}
}
sscanf(str, "%[a-z^ ],%[a-z^ ],%[a-z^ ]", tmp[0], tmp[1], tmp[2]);
//半角スペースを取り除く
for (int j = 0; j<3; j++) {
if (tmp[j][strlen(tmp[j]) - 1] == ' ') {
tmp[j][strlen(tmp[j]) - 1] = '\0';
}
}
printf("%s %s %s\n", tmp[0], tmp[1], tmp[2]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment