← cproj

ds2 and return array from function call

a4ee83efa0b6b3814b29c655b6868cf84fd517a0 · ovidiucs · Dec 2, 2019 16:57

+49 −20 · 2 files changed

  • graph/db/codevault/ret_array_from func.c +21 −0
  • graph/ds/ds2.c +28 −20
diff --git a/graph/db/codevault/ret_array_from func.c b/graph/db/codevault/ret_array_from func.c
new file mode 100644
index 0000000..9ead208
--- /dev/null
+++ b/graph/db/codevault/ret_array_from func.c	
@@ -0,0 +1,21 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+void getNextFive(int x, int *result) {
+  int arr[5];
+  int i;
+  for ( i=0;i<5;i++ ) {
+      result[i] = x+i+1;
+  }
+}
+
+int main (int argc, char** argv) {
+    int x = 5;
+    int arr[5];
+    getNextFive(x, arr);
+      int i;
+      for ( i=0;i<5;i++ ) {
+      fprintf(stdout,"Value of arr is: %d\n", arr[i]);
+  }
+   return EXIT_SUCCESS;
+}
diff --git a/graph/ds/ds2.c b/graph/ds/ds2.c
index 0e89618..16c8274 100644
--- a/graph/ds/ds2.c
+++ b/graph/ds/ds2.c
@@ -3,12 +3,20 @@
 
 #define STRSIZE 80
 char string[STRSIZE] = "test__";
-char s1[STRSIZE] = "100 test1";
-char s2[STRSIZE] = "200 test2";
+char s1[STRSIZE] = "0000010";
+char s2[STRSIZE] = "010";
 
 int strln(char str[]);
 int strpos(char str1[],char str2[]);
 
+
+int main () {
+  fprintf(stdout,"String length is: %d\n",strln(string));
+  fprintf(stdout,"String position is: %d\n",strpos(s1,s2));
+  return EXIT_SUCCESS;
+}
+
+
 int
 strln(char str[]) {
 /*****
@@ -27,31 +35,31 @@ int
 strpos(char str1[],char str2[]) {
   /*****
   * Param: two strings
-  *	Return: integer, starting location of second string
+  *	Return: integer, starting location of
+  * first occurence of the second parameters string withing the first parameter string
   *****/
 
-  int len1;
-  int len2;
-  int i;
-  int j1;
-  int j2;
+  int len1 = 0;
+  int len2 = 0;
+  int i    = 0;
+  int j1   = 0;
+  int j2   = 0;
 
   len1 = strln(str1);
   len2 = strln(str2);
-
-  for ( i=0; i+len2 <= len1; i++ ) {
-      for ( j1=i,j2=0;j2 <= len2 && s1[j1] == s2[j2] ; j1++, j2++ ) {
-        if ( j2 == len2 ) {
+  printf("\nLen 1 is %d\tLen 2 is %d\n", len1, len2);
+  for ( i=0;i+len2 <= len1; i++) {
+      printf("\n i is %d\t  i+len2 is %d\t len1 is: %d\t\n", i,i+len2,len1);
+      for ( j1=i,j2=0;(j2 <= len2) && (s1[j1] == s2[j2]); j1++, j2++ ) {
+        printf("\n j1 is %d\t  j2 is %d\t s1[j1] == s2[j2] %d\t j2+len2 is %d\t len 2 is: %d\t\n", j1,j2,(s1[j1] == s2[j2]),j2+len2, len2);
+        printf("\t\t\t\t s1[j1] is %c s2[j2] is %c\n", s1[j1],s2[j2]);
+       if ( j2 == len2 ) {
+          printf("\n j2 == len2 return %d\n\n\n", i);
             return(i);
         }
-      }
+     //   printf("\n j1 is %d\t  j2 is %d\t len2 is \t %d\n", j1,j2, len2);
     }
-    return(-1);
-
-}
 
-int main () {
-   fprintf(stdout,"String length is: %d\n",strln(string));
-   fprintf(stdout,"String position is: %d\n",strpos(s1,s2));
-   return EXIT_SUCCESS;
+  }
+             return(-1);
 }