fork(2) download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. int getCharIndex(char c)
  5. {
  6. return int (c - 'a');
  7. }
  8.  
  9. int getLargestIndex(char *str, int &length)
  10. {
  11. int count = 0;
  12. int i;
  13. int strlength = strlen(str);
  14. int indexpos = 0;
  15. int *charCount = (int *)calloc(26,sizeof(int));
  16. for(i=0;i<strlength;)
  17. {
  18. if(charCount[getCharIndex(*(str+i))] == 0)
  19. {
  20. charCount[getCharIndex(*(str+i))]++;
  21. count++;
  22. i++;
  23. }
  24. else
  25. {
  26. charCount = (int *)calloc(26,sizeof(int));
  27. length =count;
  28. indexpos= i - length;
  29. count = 0;
  30. }
  31. }
  32. return indexpos;
  33. }
  34.  
  35. int main()
  36. {
  37. int length =0;
  38. char *str = "abcdefgabcdefghabcdefghiabcdefghijkab";
  39. printf("\nInput String: %s", str);
  40. int index = getLargestIndex(str, length);
  41. printf("\nLongest Substring is : %d\n", length);
  42. for(int i=0;i<length;i++)
  43. {
  44. printf("%c", str[index++]);
  45. }
  46. getchar();
  47. return 0;
  48. }
  49.  
  50.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
Input String: abcdefgabcdefghabcdefghiabcdefghijkab
Longest Substring is : 11
abcdefghijk