Created
February 15, 2021 22:15
-
-
Save jonathan-fielding/b797f7a7924e0f6e5ecc433a7827aa3a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strings" | |
) | |
func main() { | |
var str string | |
// Print our request to the user | |
fmt.Println("Enter string to search?") | |
// Request an number from the user and store it | |
// in the nth variable | |
fmt.Scanf("%s", &str) | |
// Define variables to store the longest known string | |
// and the current test string | |
var longestString string = "" | |
var currentString string = "" | |
// Loop through each of the characters in the string | |
for i := 0; i < len(str); i++ { | |
// Get the next character | |
var char string = string(str[i]) | |
// Check to see if the current test string contains | |
// the next character | |
if strings.Contains(currentString, char) { | |
// See if the string we have finished | |
// finding is the longest string | |
if currentString > longestString { | |
longestString = currentString | |
} | |
// Split the string on the repeated character | |
currentString = strings.Split(currentString, char)[1] | |
} | |
// Add the next character to our current string | |
currentString = currentString + char | |
} | |
// Output the longest string | |
fmt.Println("The longest string is:") | |
if len(currentString) > len(longestString) { | |
fmt.Println(currentString) | |
} else { | |
fmt.Println(longestString) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment