You can use the function strstr(), this function checks the existence of a substring in a string, then just count the number of characters it has in the substring comparing to the string, or also the function strncmp()
If to check only from the beginning, Ex:
112
1125468
In this case you can cycle and traverse character by character, if different give a break
and concludes that it is not Subsequence
It will also be necessary to allocate memory of 10^10
and 10^32
in the 2 strings, as required by the exercise.
Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
char N1[100], N2[100];
int count=0, x=0;
scanf(" %s",N1);
scanf(" %s", N2);
do
{
if(!strncmp(N2+x, N1, strlen(N1)))
{
count++;
printf("N1 e´ uma substring de N2\n");
printf("Qtd.Subsequencias %d\n", count);
x+=strlen(N1);
}
else
x+=1;
} while(x<strlen(N2));
}
I think that’s what you want to do, obviously we have to print the cycle, but it’s just one example
STDIN
12
123123123123
STDOUT
N1 e´ uma substring de N2
Qtd.Subsequencias 4
As we can see, in the last iteration Qtd.Subsequences is equal to 4.
In this case the fastest way would be to use the function strncmp()
I understood how the strstr() function works, but how do I make this loop and count? how many substring do you have in the string...
– Misaee 21
@Misaee21 I will edit my answer with the code I made, please check.
– Fábio Morais
It is kind of different from the answers there of the site test this entry. STDIN 12 STDOUT 1231321455123214565423112
– Misaee 21
@Misaee21 You are right, I will edit with the correct solution
– Fábio Morais
I managed to solve it, but I had to use the function the guy indicated down there... if you want to see my code just say.
– Misaee 21
@Misaee21 I had already edited my answer with this hiccup
– Fábio Morais
I don’t get it, you’ve done it?
– Misaee 21
@Misaee21 See the code that is in my answer to your question, make exactly the request, I had edited and put an improved code
– Fábio Morais