Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include < string.h >
- #include <stdlib.h>
- void small_cap(char* str, char** str_small, char** str_cap);
- int main()
- {
- char str[] = "AaBb1231_Cc_)!@#B";
- char* str_small, * str_cap;
- small_cap(str, &str_small, &str_cap);
- printf("\nThe string is: %s\n", str);
- if (str_small == NULL || str_cap == NULL) // check null
- {
- printf("allocation failed");
- return 1;
- }
- printf("\nCap: %s\nSmall: %s\n\n", str_cap, str_small);
- free(str_small);
- free(str_cap);
- return 0;
- }
- void small_cap(char* str, char** str_small, char** str_cap)
- {
- int len = strlen(str);
- int countC = 0, countS = 0, j = 0, z = 0;
- for (int i = 0; i < len; i++)
- {
- if (str[i] >= 'a' && str[i] <= 'z')
- countS++;
- else if (str[i] >= 'A' && str[i] <= 'Z')
- countC++;
- }
- *str_small = (char*)malloc(1 + countS * sizeof(char));
- *str_cap = (char*)malloc(1 + countC * sizeof(char));
- if (*str_small && *str_cap)
- {
- for (int i = 0; i < len; i++)
- {
- if (str[i] >= 'a' && str[i] <= 'z')
- (*str_small)[j++] = str[i];
- else if (str[i] >= 'A' && str[i] <= 'Z')
- (*str_cap)[z++] = str[i];
- }
- (*str_small)[j] = '\0';
- (*str_cap)[z] = '\0';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement