Substring program

Asked

Viewed 140 times

1

I have a C program that displays all 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] = '\0';
        todas_substrings(str);
    }
}


int main(int argc, char const *argv[]) {
    string str = "UTFPR";

    todas_substrings(str);

    return 0;
}

Which has as a result :

UTFPR
TFPR
FPR
PR
R
UTFP
TFP
FP
P
UTF
TF
F
UT
T
U

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

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);
    }
}

But the following mistake is being made :

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)

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

  • What exactly do you want to do? I don’t understand the motivation, it has to have an order for printing?

2 answers

4

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

Java does not have this inefficiency, but there is another.

Not that it’s a problem for an exercise, but it’s good to know that unlike C, you’ll have several memory allocations to accomplish this task and it’s not suitable in more real cases to program this way. Interestingly in Java it is more interesting to do by hand than using substring(). But by doing what you ask in 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");
    }
}

Behold working in the ideone. And in the repl it.. Also put on the 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] = '\0';
            printf("%s\n", str + i);
        }
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Can you do without putting the terminator and solve only in printf() limiting right there to where to go.

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

3


If you already have the C code working, just translate it directly to Java. You don’t need to redo anything completely other than scratch.

See the translation:

  • putsSystem.out.println.

  • stringString.

  • str + 1str.substring(1).

  • strlen(str)str.length().

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

  • todas_substringstodasSubstrigs.

See how it looks:

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.

Browser other questions tagged

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