Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. /* Write a function called findString() to determine
  2. if one charcther string exists inside of another string.
  3. The first argument of the function shuld be the charcater
  4. string to be searched and the second argument the string
  5. you are intrested in finding. If the function finds the specified
  6. string have it return the location int the source string
  7. where the string was found. If the function dosent find
  8. the string have it return -1. */
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdbool.h>
  13.  
  14. bool equal_strings(const char s1[], const char s2[])
  15. {
  16. int i = 0;
  17. bool are_equal;
  18.  
  19. while( s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0')
  20. ++i;
  21. if( s1[i] == '\0' && s2[i] == '\0')
  22. {
  23. are_equal = true;
  24. }
  25. else
  26. {
  27. are_equal = false;
  28. }
  29.  
  30. return are_equal;
  31.  
  32.  
  33. }
  34.  
  35.  
  36. int compare_strings( char s1[], char s2[])
  37. {
  38. int i , j , count, k = 0;
  39. int string_start_position, string_end_position;
  40. char haystack[81];
  41. char nedele[81];
  42. char temp_haystack[81];
  43. char null_zero[] = {"\0"};
  44. bool found_nedele = 0;
  45.  
  46. int original_string_length;
  47. int second_string_length;
  48.  
  49. original_string_length = strlen(s1);
  50. //printf(" %d", original_string_length);
  51. second_string_length = strlen(s2);
  52.  
  53. for( i = 0; i < original_string_length; ++i)
  54. {
  55. haystack[i] = s1[i];
  56. //printf("%c", haystack[i]);
  57.  
  58. if(haystack[i] == s2[0] )
  59. {
  60. string_start_position = i;
  61. //printf(" %d", string_start_position);
  62.  
  63. for( j = 0; j < second_string_length ; ++j)
  64. {
  65. string_end_position = j;
  66. //printf("%d", j);
  67. nedele[j] = s2[j];
  68. //printf("%c", nedele[j]);
  69. k = (string_start_position + string_end_position) ;
  70. //printf("%d", k-1);
  71. if(haystack[string_start_position+string_end_position ] != nedele[string_end_position ])
  72. {
  73. break;
  74. //printf("%d", i+j);
  75. }
  76. else
  77. {
  78. found_nedele = 1;
  79. printf("%d", found_nedele);
  80. temp_haystack[i] = haystack[j];
  81. strcat(temp_haystack, null_zero);
  82. strcat(nedele, null_zero);
  83. }
  84. }
  85.  
  86. }
  87.  
  88. }
  89.  
  90.  
  91. if(found_nedele)
  92. {
  93. equal_strings(nedele,temp_haystack);
  94.  
  95. if(equal_strings)
  96. {
  97. printf("The strings are true\n");
  98. printf("%d",string_start_position);
  99.  
  100. }
  101. else
  102. {
  103. printf("ai vrea tu\n");
  104. }
  105. }
  106.  
  107.  
  108.  
  109.  
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116. int main(void)
  117. {
  118. char haystack[] = {"chaterbox"};
  119. char nedele[] = {"box"};
  120. int result;
  121.  
  122. result = compare_strings(haystack, nedele);
  123. //printf("%d", result);
  124.  
  125.  
  126. return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement