Advertisement
DimasDark

L0Q4 Fixed

May 27th, 2013
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. char _str[1000];
  5. char _c;
  6.  
  7. void divideString(char str[], char c)
  8. {
  9.     char *ptr; //apontador
  10.    
  11.     ptr = strchr(str, c); //aponta para primeira ocorrência do char que eu quero
  12.    
  13.     if (ptr == NULL)   //Se aponta pra algo != null faz o processo
  14.     {
  15.         return;
  16.     }
  17.     else
  18.     {
  19.         char p1[1000];
  20.         char p2[1000];
  21.        
  22.         *ptr = '\0';
  23.        
  24.         strcpy(p1, str); //copia a string baseado na ocorrencia e armazena em p1
  25.        
  26.         strcpy(p2, ptr+1);
  27.        
  28.         if (p1 != NULL && strlen(p1) >= 1)
  29.                 {
  30.             printf("\n%s", p1);
  31.                 divideString(p1, c-1);
  32.                 }
  33.         if (p2 != NULL && strlen(p2) >= 1)
  34.                 {
  35.                 printf("\n%s", p2);
  36.                 divideString(p2, c+1);
  37.                 }
  38.        
  39.         return;
  40.     }
  41. }
  42.  
  43. int main()
  44. {
  45.     freopen("L0Q4.in", "r", stdin);
  46.     freopen("L0Q4.out", "w", stdout);
  47.    
  48.     while (scanf("%s %c", _str, &_c) != EOF)
  49.     {
  50.         divideString(_str, _c);
  51.         printf("\n");
  52.         strcpy(_str, ""); /* limpa _str */
  53.     }
  54.    
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement