How to count how many substrings you have in a C string?

Asked

Viewed 1,096 times

0

https://www.urionlinejudge.com.br/judge/pt/problems/view/2126

This exercise I thought better read as string or not...

#include <stdio.h>

int main(void) {
char N1,quantidadesdeN1;

while(scanf("%s",&N1)!=EOF)
   {
     scanf("%s",&quantidadesdeN1);

     /*Como faço pra ver quantos N1 tem na variável quantidadesdeN1 com strcmp ?No strcmp tem jeito de pegar apenas partes da quantidadedeN1? Se caso não for possível resolver no strcmp tem outro jeito de resolver ?
     */
   }
return 0;
} 

2 answers

0

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...

  • @Misaee21 I will edit my answer with the code I made, please check.

  • It is kind of different from the answers there of the site test this entry. STDIN 12 STDOUT 1231321455123214565423112

  • @Misaee21 You are right, I will edit with the correct solution

  • 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.

  • @Misaee21 I had already edited my answer with this hiccup

  • I don’t get it, you’ve done it?

  • @Misaee21 See the code that is in my answer to your question, make exactly the request, I had edited and put an improved code

Show 3 more comments

0


in the library you have a function called strncmp(str1, str2, numero_de_casas)

str1 => variable 1 str2 => variable 2 numero_de_casas => you put how many houses you want for comparison.

Place the function in a loop of repetition, which modifies the initial position of str2, a counting variable for the times that were found to str1, and finally a variable for first position

https://www.tutorialspoint.com/c_standard_library/c_function_strncmp.htm

  • I had to use strlen() to count house numbers. At loop time how do I go to change the starting position of str2 ?

  • When you call str2 (or the name of the variable string) you are actually referencing the position str2[0] and the routine reads as many boxes as needed, if you put str2+1 you will be referencing str2[1]then use the repetition control variable to go increasing the position you want to start

  • I can’t understand...#include <stdio. h> #include <string. h> int main(void) { char N1[10],quantityL1[30]; while(scanf("%s",&N1)!=EOF) { int cont = strlen(N1),i,subsequences=0; scanf("%s",&quantityTN1); int cont2 = strlen(quantityTN1); for(i=0; i<cont2; i++) { if(strncmp(N1,quantityesdeN1[i],cont)=0) subsequences++ } } Return(0); ;}

  • Put quantityN1+i instead of quantityN1[i], because in its form it is just pegandl a character, in the first way it says that the string starts with quantityN1[0] and in the next cycle starts with quantityN1[1]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.