Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int isValid(char* str)
  5. {
  6.     int flag = 0;
  7.     if (*str >= '0' && *str <= '9')
  8.         return 0;
  9.     while (*str)
  10.     {
  11.         if (flag == 0)
  12.         {
  13.             if (!((*str >= 'a' && *str <= 'z') || (*str >= 'A' && *str <= 'Z') || (*str >= '0' && *str <= '9')))
  14.             {
  15.                 return 0;
  16.             }
  17.             if ((*str >= '0' && *str <= '9'))
  18.                 flag = 1;
  19.         }
  20.         str++;
  21.     }
  22.     if (flag == 0)
  23.         return 0;
  24.     else
  25.         return 1;
  26. }
  27.  
  28. char* fun(char* str)
  29. {
  30.     int count = 0;
  31.     char* tmp,*num,*newStr;
  32.     tmp = str;
  33.     if (isValid(str) == 0)
  34.         return NULL;
  35.     while (*tmp != NULL && !(*tmp >= '0' && *tmp <= '9'))
  36.     {
  37.         count++;
  38.         tmp++;
  39.     }
  40.     //tmp points to the desired num
  41.     num = tmp;
  42.     newStr = (char*)calloc(((int)(*num - '0') * count) + 1, sizeof(char));
  43.     if (!newStr)
  44.         exit(1);
  45.     tmp = str;
  46.     for (int i = 0,k=0; i < (int)(*num - '0'); i++)
  47.     {
  48.         for (int j = 0; j < count; j++)
  49.         {
  50.             *(newStr + (k++)) = *(tmp + j);
  51.         }
  52.     }
  53.     return newStr;
  54. }
  55.  
  56. void main()
  57. {
  58.     char* str = "Good567Day",*ptr;
  59.     ptr = fun(str);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement