Created
February 1, 2017 04:07
-
-
Save dvtate/63feea3e3f5534a3090b07399025e024 to your computer and use it in GitHub Desktop.
the solution to the challenge faced by my friend Umar Faroq
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
| #include <stdio.h> | |
| #include <stdlib.h> | |
| int main(){ | |
| // prompt: | |
| printf("please enter a string:"); | |
| // get input | |
| size_t linelen = 500; | |
| char* line = (char*) malloc(linelen); | |
| if (getline(&line, &linelen, stdin) == -1) { | |
| printf("Error: no input\n"); | |
| return -1; | |
| } | |
| // these are used to track which is the longest | |
| char* currLongestStart = line; // points to the start of the substring | |
| size_t currLongestLen = 1; // how long the substring is | |
| // find the longest | |
| while (*line) { | |
| char* temp = line++; | |
| // while the next letter is lower in the alphabet | |
| while (*line && *(line-1) <= *line) { | |
| line++; | |
| } | |
| // if the new one is longer | |
| if ((line - temp) > currLongestLen) { | |
| currLongestStart = temp; | |
| currLongestLen = (line - temp); | |
| // if they have the same length pick the one with the first letter | |
| } else if ((line - temp) == currLongestLen && *temp < *currLongestStart) { | |
| currLongestStart = temp; | |
| currLongestLen = (line- temp); | |
| } | |
| } | |
| // print the output | |
| printf("%.*s\n", currLongestLen, currLongestStart); | |
| } |
Author
dvtate
commented
Feb 1, 2017

Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment