Advertisement
cd62131

input alnum, remove alpha, concat num

May 23rd, 2019
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. int main(void) {
  6.   setbuf(stdout, NULL);
  7.   puts("英字を削除します。");
  8.   char s[2][15 + 1];
  9.   for (int i = 0; i < 2; ++i) {
  10.     printf("%d つめの文字列を入力\t: ", i + 1);
  11.     char buf[BUFSIZ];
  12.     if (!fgets(buf, BUFSIZ, stdin)) { exit(1); }
  13.     for (char *p = buf; *p; ++p) {
  14.       if (!isalnum(*p)) { *p = '\0'; }
  15.     }
  16.     int len = strlen(buf);
  17.     if (15 < len) {
  18.       puts("入力された文字列が多いです。"), --i;
  19.       continue;
  20.     }
  21.     for (char *p = buf, *q = s[i]; *p || (*q = '\0'); ++p) {
  22.       if (isdigit(*p)) { *q++ = *p; }
  23.     }
  24.     printf("英字の個数\t\t: %d\n", len - (int)strlen(s[i]));
  25.     printf("削除後の文字列\t\t: %s\n", s[i]);
  26.   }
  27.   char t[15 * 2 + 1];
  28.   if (strlen(s[1]) <= strlen(s[0])) {
  29.     sprintf(t, "%s%s", s[0], s[1]);
  30.   } else {
  31.     sprintf(t, "%s%s", s[1], s[0]);
  32.   }
  33.   printf("連結された文字列は %s です。\n", t);
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement