+ 5

How can I find position of one array in another (C) ?

I have an array and I want to find what is position of another array in it .

31st Dec 2018, 11:29 AM
San Gvinik
5 Respostas
+ 6
strstr() will find the position of one string (char array) in another but not the position of one generic array in another.
1st Jan 2019, 11:02 PM
Sonic
Sonic - avatar
+ 4
I made code that make auto uppercasing and I want pepole to enter any other words that need to be uppercased in their text . For a now this code looks like that : #include <stdio.h> #include <stdlib.h> int main() { int j=-1; char s [256]; do {j++;scanf("%c",&s[j]);} while (s[j]!='\n'); s[0]=s[0]-32; for (int i=0;s[i]!='\n';i++){ if (s[i]=='.'||s[i]=='\"'){i++; if (s[i]==' ')i++; else s[i]=s[i]-32;}} j=-1; do {j++;printf("%c",s[j]);} while (s[j]!='\n'); return 0; }
31st Dec 2018, 2:09 PM
San Gvinik
+ 2
I have found a method . Thanks everyone , who wanted to help . You can do it with the help of: Pointer= strstr ( StringWhereToFind , StringToFind )(You'll need do include in your program string.h file)
1st Jan 2019, 12:28 AM
San Gvinik
+ 1
Have you written a code? If yes, can you share it please, then I can help you.
31st Dec 2018, 12:54 PM
Théophile
Théophile - avatar
0
So code should look like something like that ? int /*(for example)*/ array1[10] , array2[5] , same=0 , position; for (int i=0;i<sizeof(array1)/ sizeof(array1[0]);i++){ if (array1[i]==array2[i]){ for (int j=i;j<sizeof(array2)/ sizeof(array2[0]);j++){ if (array1[j]==array2[j])same++; else same=0; } }if (same==sizeof(array2)/sizeof(array2[0]))position=i; } Is there any other , simplier way to do it?
2nd Jan 2019, 4:36 PM
San Gvinik