Last active
May 1, 2016 10:53
-
-
Save otaks/ab1500c32f99a58c3bfce10be2ed5b85 to your computer and use it in GitHub Desktop.
sscanf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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