Substring Program

1

I have a C program that displays all the substrings of a String:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef char string[100];

void substrings(string str) {
    if (strlen(str) >= 1) {
        puts(str);
        substrings(str + 1);
    }
}

void todas_substrings(string str) {
    int tam = strlen(str);
    if (tam >= 1) {
        substrings(str);
        str[tam - 1] = '
UTFPR
TFPR
FPR
PR
R
UTFP
TFP
FP
P
UTF
TF
F
UT
T
U
'; todas_substrings(str); } } int main(int argc, char const *argv[]) { string str = "UTFPR"; todas_substrings(str); return 0; }

That results in:

public class Substring {

    public void todasSubstrings(String str) {
        int tamanho1 = 0;
        int tamanho2 = str.length();

        for(int i = 0; i < str.length(); i++) {
            for(int j = 0; j < str.length(); j++) {
                System.out.println(str.substring(tamanho1,tamanho2));
                tamanho2--;
            }
            tamanho1++;
        }
    }

    public static void main(String[] args) {
        Substring sb = new Substring();

        String str = "UTFPR";

        sb.todasSubstrings(str);
    }
}

However, the challenge now is to pass the same program to Java, but since Java already has a function for it, I did this:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 1, end 0, length 5
        at java.base/java.lang.String.checkBoundsBeginEnd(Unknown Source)
        at java.base/java.lang.String.substring(Unknown Source)
        at Substring.todasSubstrings(Substring.java:10)
        at Substring.main(Substring.java:22)

But it is giving the following error:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef char string[100];

void substrings(string str) {
    if (strlen(str) >= 1) {
        puts(str);
        substrings(str + 1);
    }
}

void todas_substrings(string str) {
    int tam = strlen(str);
    if (tam >= 1) {
        substrings(str);
        str[tam - 1] = '
UTFPR
TFPR
FPR
PR
R
UTFP
TFP
FP
P
UTF
TF
F
UT
T
U
'; todas_substrings(str); } } int main(int argc, char const *argv[]) { string str = "UTFPR"; todas_substrings(str); return 0; }

What would be this error and how could you fix it to do the same function of the program in C?

    
asked by anonymous 17.08.2018 / 22:33

2 answers

2

If you already have the C code working, just translate directly to Java. You do not need to redo anything completely other than zero.

See the translation:

  • putsSystem.out.println .

  • stringString .

  • str + 1str.substring(1) .

  • strlen(str)str.length() .

  • str[tam - 1] = 'str = str.substring(0, str.length() - 1);';todas_substrings .

  • todasSubstrigs → %code% .

Here's how:

class Substring {

    private static void substrings(String str) {
        if (str.length() >= 1) {
            System.out.println(str);
            substrings(str.substring(1));
        }
    }

    private static void todasSubstrings(String str) {
        int tam = str.length();
        if (tam >= 1) {
            substrings(str);
            str = str.substring(0, str.length() - 1);
            todasSubstrings(str);
        }
    }

    public static void main(String[] args) {
        todasSubstrings("UTFPR");
    }
}

See here working on ideone.

    
17.08.2018 / 22:51
2

C also has and more efficient than it was used, so I always talk to use what you have ready, but that is:)

Java does not have this inefficiency, but it has another.

Not that it is a problem for an exercise, but it is good to know that, unlike C, you will have multiple memory allocations to perform this task and it is not appropriate in more realistic cases to program this way. Interestingly in Java it is more interesting to do at hand than using substring() . But doing what you ask in the question:

class Substring {
    public static void todasSubstrings(String str) {
        for (int j = str.length(); j >= 0; j--) for (int i = 0; i < j; i++) System.out.println(str.substring(i, j));
    }
    public static void main(String[] args) {
        todasSubstrings("UTFPR");
    }
}

See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .

If you want to do better in C:

#include <stdio.h>
#include <string.h>

int main(void) {
    char str[4] = "UTFPR";
    for (int j = strlen(str); j >= 0; j--) {
        for (int i = 0; i < j; i++) {
            str[j] = '
class Substring {
    public static void todasSubstrings(String str) {
        for (int j = str.length(); j >= 0; j--) for (int i = 0; i < j; i++) System.out.println(str.substring(i, j));
    }
    public static void main(String[] args) {
        todasSubstrings("UTFPR");
    }
}
'; printf("%s\n", str + i); } } }

See running on ideone . And no Coding Ground . Also I put it in GitHub for future reference .

You can do without putting the terminator and solving only in printf() limiting right where you go.

But I would avoid using strlen() which is inefficient, in this case just replace it with the size already known , if it were a function I would pass the known size, or I would use a structure that stores the string size as people usually do in production.

    
17.08.2018 / 23:20