Advertisement
ppupil2

PE 04.01.20 Q6

Mar 17th, 2022
783
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. /* Tra ve index tim thay cua c trong mang arr[]
  7.    Neu khong thay: tra ve -1 */
  8. int indexOf(char c, char arr[], int size) {
  9.     int i;
  10.    
  11.     for (i = 0; i < size; i++) {
  12.         if (arr[i] == c) {
  13.             return i;
  14.         }
  15.     }
  16.     return -1;
  17. }
  18.  
  19. int main() {
  20.   system("cls");
  21.   //INPUT - @STUDENT:ADD YOUR CODE FOR INPUT HERE:
  22.     char s[51];
  23.     gets(s);
  24.   // Fixed Do not edit anything here.
  25.   printf("\nOUTPUT:\n");
  26.   //@STUDENT: WRITE YOUR OUTPUT HERE:
  27.     char characters[51];
  28.     int count[51];
  29.     int i, size = 0, index;
  30.  
  31.     for(i = 0; i < strlen(s); i++) {
  32.         if (s[i] >= 97 && s[i] <= 122) { // a-z
  33.             /* check xem co trong mang characters hay chua?
  34.                neu co roi: tang count 1
  35.                neu chua co: them vao mang characters */
  36.             index = indexOf(s[i], characters, size);
  37.             if (index != -1) { // tim thay ~> tang count 1
  38.                 count[index]++;
  39.             } else {
  40.                 characters[size] = s[i];
  41.                 count[size] = 1;
  42.                 size++;
  43.             }
  44.         }
  45.     }
  46.    
  47.     printf("%c_%d", characters[0], count[0]);
  48.     for(i = 1; i < size; i++) {
  49.         printf(" %c_%d", characters[i], count[i]);
  50.     }
  51.   //--FIXED PART - DO NOT EDIT ANY THINGS HERE
  52.   printf("\n");
  53.   system ("pause");
  54.   return(0);
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement