some struct problem

Currently, I am working on an assignment. but I encounter a problem of returning the value of longest.start longest.length.
The main part must not be changed. I can only modify longestSubstring part.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#define MAX_LENGTH 1000      // the max allowed input string length

struct Substring {          // the structure used to store the function output
    int start;              // the start position of the longest substring
    int length;             // the length of the longest substring
};

Substring longestSubstring(char *str) {
     // how can I return start and length value to main function?
}

int main(int argc, char** argv) {

    char str[MAX_LENGTH]; // buffer

    ifstream fin("tut02_input.txt");
    if (!fin) {
        cout << "Input file not found.";
        exit(1);
    }

    int testcase = 0;
    fin >> testcase;

    for (int i = 0; i < testcase; i++) {
        fin >> str;
        Substring longest = longestSubstring(str);
        cout << "Case " << i + 1 << endl;
        printf("The substring (%d,%d) is %.*s\n", longest.start, longest.length, longest.length, str + longest.start); 
        cout << endl;
    }

    fin.close();

    return 0;
} 
Last edited on
What's the length of a piece of string?

You haven't explained what you actually want to do. After all, the longest substring of a given string ... is that string itself.
The requirement of assignment is below.
/*
* Given a c-string str, find the last occurrence of the longest substring without repeating characters.
* Return a Substring object having the start position and the length of the longest substring.
* Precondition: the input string str is non-empty and contains ASCII characters only.
* Time Complexity Requirement: O(n)
*/


The txt file is given below.
5
aaaa
abababa
abccba
CPPisFun
theQUICKbrownFOXjumpsOVERtheLAZYdog

================================================================================
Below is the description of the input format and expected output.
================================================================================

Input:
The first line of input contains an integer T denoting the number of test cases.
Each following line is a test case that contains a string of ASCII characters.

Output:
Case 1
The substring (3,1) is a

Case 2
The substring (5,2) is ba

Case 3
The substring (3,3) is cba

Case 4
The substring (2,6) is PisFun

Case 5
The substring (0,21) is theQUICKbrownFOXjumps
I just want to know how can I return value from Substring longestSubstring to the main function,
I have tried longestSubstring.start and Substring::start , it did not work.
Your function declares that it will return an entity of type Substring. So, create the appropriate entity of type Substring and return that.

You simply need to find the data members start and length, put them in a Substring, return that and Bob's your uncle.
Last edited on
ok i will try, thanks!
Topic archived. No new replies allowed.