Skip to content

Instantly share code, notes, and snippets.

@dvtate
Created February 1, 2017 04:07
Show Gist options
  • Save dvtate/63feea3e3f5534a3090b07399025e024 to your computer and use it in GitHub Desktop.
Save dvtate/63feea3e3f5534a3090b07399025e024 to your computer and use it in GitHub Desktop.
the solution to the challenge faced by my friend Umar Faroq
#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);
}
@dvtate
Copy link
Author

dvtate commented Feb 1, 2017

image

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