Skip to content

Instantly share code, notes, and snippets.

@rich-97
Last active November 10, 2016 19:06
Show Gist options
  • Save rich-97/8dfe7b65fc4e9cd84dbbcccf6a53b41a to your computer and use it in GitHub Desktop.
Save rich-97/8dfe7b65fc4e9cd84dbbcccf6a53b41a to your computer and use it in GitHub Desktop.
the longest word
#include "stdio.h"
#include "string.h"
int count = 0;
int max_len = 0;
int count_comp = 0;
int count_word = 0;
int count_index = 0;
int len = strlen(str);
int arr_nword[count_word];
int arr_index[count_index];
char str[] = "example example2 proof guide";
int main () {
for (int i = 0; i <= len; i++) {
if (str[i] == ' ' || str[i] == '\0') {
arr_nword[count_word] = count;
count_word++;
count = 0;
} else count++;
}
for (int i = 0; i < count_word; i++) {
if (arr_nword[i] > max_len)
max_len = arr_nword[i];
}
for (int i = 0; i <= len; i++) {
if (str[i] == ' ' || str[i] == '\0') {
if (count_comp == max_len) {
arr_index[count_index] = i;
count_index++;
}
count_comp = 0;
} else count_comp++;
}
for (int i = 0; i < count_index; i++) {
puts("the longest word is: ");
for (int j = (arr_index[i] - max_len); j < arr_index[i]; j++)
printf("%c", str[j]);
printf("\n");
}
return 0;
}
string = 'example example2 proof guide'
listStr = string.split()
listLength = []
for i in listStr:
listLength.append(len(i))
for i in listStr:
if (max(listLength) == len(i)):
print i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment