Advertisement
Rejack

Seperate small letters and capped letters

Apr 12th, 2024
554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include < string.h >
  3. #include <stdlib.h>
  4.  
  5. void small_cap(char* str, char** str_small, char** str_cap);
  6.  
  7. int main()
  8. {
  9.     char str[] = "AaBb1231_Cc_)!@#B";
  10.  
  11.     char* str_small, * str_cap;
  12.  
  13.     small_cap(str, &str_small, &str_cap);
  14.  
  15.     printf("\nThe string is: %s\n", str);
  16.  
  17.     if (str_small == NULL || str_cap == NULL)                   // check null
  18.     {
  19.         printf("allocation failed");
  20.         return 1;
  21.     }
  22.    
  23.     printf("\nCap: %s\nSmall: %s\n\n", str_cap, str_small);
  24.  
  25.     free(str_small);
  26.     free(str_cap);
  27.  
  28.     return 0;
  29. }
  30.  
  31. void small_cap(char* str, char** str_small, char** str_cap)
  32. {
  33.     int len = strlen(str);
  34.     int countC = 0, countS = 0, j = 0, z = 0;
  35.  
  36.     for (int i = 0; i < len; i++)
  37.     {
  38.         if (str[i] >= 'a' && str[i] <= 'z')
  39.             countS++;
  40.  
  41.         else if (str[i] >= 'A' && str[i] <= 'Z')
  42.             countC++;
  43.     }
  44.  
  45.     *str_small = (char*)malloc(1 + countS * sizeof(char));
  46.     *str_cap = (char*)malloc(1 + countC * sizeof(char));
  47.  
  48.     if (*str_small && *str_cap)
  49.     {
  50.         for (int i = 0; i < len; i++)
  51.         {
  52.             if (str[i] >= 'a' && str[i] <= 'z')
  53.                 (*str_small)[j++] = str[i];
  54.  
  55.             else if (str[i] >= 'A' && str[i] <= 'Z')
  56.                 (*str_cap)[z++] = str[i];
  57.         }
  58.  
  59.         (*str_small)[j] = '\0';
  60.         (*str_cap)[z] = '\0';
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement