fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6.  
  7.  
  8. int IndexOf(char *texto,char c, int vez)
  9. {
  10. int i =0;
  11. int index =0;
  12. while (texto[index]!='\0')
  13. {
  14. if (texto[index]==c)
  15. {
  16. i++;
  17. if (i==vez)
  18. return index;
  19. }
  20.  
  21. index++;
  22. }
  23. return -1;
  24. }
  25.  
  26. ///Subdivide uma string
  27. char *SubString(char *texto, int inicio,int fim)
  28. {
  29. if (fim<inicio)
  30. {
  31. char *retorno = ((char*)malloc(sizeof(char)));
  32. retorno[0]='\0';
  33. return retorno;
  34. }
  35. else if (inicio==fim)
  36. {
  37. char *retorno = ((char*)malloc(sizeof(char)+1));
  38. retorno[0] = texto[inicio];
  39. retorno[1] = '\0';
  40. return retorno;
  41. }
  42. else
  43. {
  44. int i=0,j=0;
  45. int t = ((fim-inicio)+2);
  46. char *retorno = ((char*)malloc(sizeof(char)*t));
  47. while (texto[i]!='\0' && j<(t-1))
  48. {
  49. if (i>=inicio && i<=fim)
  50. {
  51. retorno[j] = texto[i];
  52. j++;
  53. }
  54. i++;
  55. }
  56.  
  57. retorno[j] = '\0';
  58. return retorno;
  59. }
  60. }
  61.  
  62.  
  63.  
  64.  
  65.  
  66. int main(void) {
  67. char linha[1000]="ALGUMA COISA,ALGO=TESTE,FIM";
  68.  
  69. printf("Linha: %s\n", linha);
  70. int i = IndexOf(linha,',',1);
  71. int f = IndexOf(linha,'=',1);
  72. int n = IndexOf(linha,',',2);
  73. printf("Campo: %s\n", SubString(linha,i+1,f-1));
  74. printf("Valor: %s\n", SubString(linha,f+1,n-1));
  75. return 0;
  76. }
  77.  
Success #stdin #stdout 0s 4568KB
stdin
Standard input is empty
stdout
Linha: ALGUMA COISA,ALGO=TESTE,FIM
Campo: ALGO
Valor: TESTE