fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void stringrev(char*s1)
  5. {
  6. char *start;
  7. char *end;
  8. if (s1==NULL)
  9. {
  10. return;
  11. }
  12. start=s1;
  13. char *temp;
  14. end=start+strlen(s1)-1;
  15. while (start<end)
  16. {
  17. *temp=*end;
  18. *end=*start;
  19. *start=*temp;
  20. start ++;
  21. end--;
  22. }
  23. }
  24.  
  25. int stringcomp(char*s1,char*s2)
  26. {
  27. if(s1==NULL && s2==NULL)
  28. {
  29. return 0;
  30. }
  31. if(s1==NULL || s2==NULL)
  32. {
  33. return -1;
  34. }
  35. while(*s1==*s2)
  36. {
  37.  
  38. if(*s1=='\0')
  39. {
  40. return 0;
  41. }
  42. s1++;
  43. s2++;
  44. }
  45. return (*s1-*s2);
  46. }
  47.  
  48. int main()
  49. {
  50. //Test cases for string reverse
  51. char test[1000]="abcdef";
  52. stringrev(test);
  53. if (stringcomp(test,"fedcba")==0)
  54. {
  55. printf("Test 1 Passed\n");
  56. }
  57. else
  58. printf("Test 1 Failed\n");
  59.  
  60. strcpy(test,"");
  61. stringrev(test);
  62. if (stringcomp(test,"")==0)
  63. {
  64. printf("Test 2 Passed\n");
  65. }
  66. else
  67. printf("Test 2 Failed\n");
  68.  
  69.  
  70. stringrev(NULL);
  71. printf("Test 3 passed\n");
  72.  
  73. strcpy(test,"123@rf^");
  74. stringrev(test);
  75. if (stringcomp(test,"^fr@321")==0)
  76. {
  77. printf("Test 4 Passed\n");
  78. }
  79. else
  80. printf("Test 4 Failed\n");
  81.  
  82. strcpy(test," a b c ");
  83. stringrev(test);
  84. if (stringcomp(test," c b a ")==0)
  85. {
  86. printf("Test 5 Passed\n");
  87. }
  88. else
  89. printf("Test 5 Failed\n");
  90.  
  91. memset(test,0,10);
  92. strncpy(test,"abcd",4);
  93. stringrev(test);
  94. if (stringcomp(test,"dcba")==0)
  95. {
  96. printf("Test 6 Passed\n");
  97. }
  98. else
  99. printf("Test 6 Failed.Returned string is %s\n",test);
  100. char test2[1000];
  101. memset(test,'c',400);
  102. memset(test+400,'b',400);
  103. test[800]='\0';
  104. memset(test2,'b',400);
  105. memset(test2+400,'a',400);
  106. test2[800]='\0';
  107. stringrev(test);
  108. if (stringcomp(test,test2)==0)
  109. {
  110. printf("Test 7 Passed\n");
  111. }
  112. else
  113. printf("Test 7 Failed\n");
  114. //Test cases for string Compare
  115. /*if (stringcomp("aaa","aaa")==0)
  116. printf("Test 1 Passed");
  117. else
  118. printf("Test 1 Failed");
  119.  
  120. if (stringcomp(" aba"," aba")==0)
  121. printf("Test 2 Passed");
  122. else
  123. printf("Test 2 Failed");
  124.  
  125. if (stringcomp("","")==0)
  126. printf("Test 2 Passed");
  127. else
  128. printf("Test 2 Failed");
  129. if (stringcomp("","aba")==-1)
  130. printf("Test 2 Passed");
  131. else
  132. printf("Test 2 Failed");
  133. if (stringcomp("aba","")==0)
  134. printf("Test 2 Passed");
  135. else
  136. printf("Test 2 Failed");
  137. if (stringcomp(" aba"," aba")==0)
  138. printf("Test 2 Passed");
  139. else
  140. printf("Test 2 Failed");
  141. */
  142. return 0;
  143. }
Success #stdin #stdout 0s 5448KB
stdin
Standard input is empty
stdout
Test 1 Passed
Test 2 Passed
Test 3 passed
Test 4 Passed
Test 5 Passed
Test 6 Passed
Test 7 Failed