Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int mystrlen(const char * str)
  6. {
  7.     int len;
  8.  
  9.     for (len = 0; str[len] != '\0'; len++);
  10.  
  11.     return len;
  12. }
  13.  
  14. int main() {
  15.     int i = 0, j, k, max = 0, flag = 0, z=0, start=0;
  16.     int len;
  17.     char *word = nullptr, *max_word = nullptr, *temp = nullptr;
  18.     puts("Input text with the $ in the end:");
  19.     max_word = (char*)malloc(sizeof(char) * 1);
  20.     temp = (char*)malloc(sizeof(char) * 1);
  21.     word = (char*)malloc(sizeof(char) * 1);
  22.     for (i = 0; (word[i] = getchar()) != '\n'; i++);
  23.     word[i] = '\0';
  24.     len = mystrlen(word);
  25.     for (i = 0; i < len+1; i++) {
  26.         //if (start == 0) {
  27.         //  for(int j = i; word[j] != ' ' && word[j] != ',' && word[j] != '\n' && word[j] != '.' && word[j] != '!' && word[j] != '?' && word[j] != ';' && word[j] != ':' && word[j] != '\0'; j++){
  28.         //  }
  29.         //  j--;
  30.         //  max_word = (char*)realloc(max_word, sizeof(char) * j);
  31.         //  max_word[j] = '\0';
  32.         //  for (int t = 0; t < j; t++) {
  33.         //      max_word[t] = word[t];
  34.         //  }
  35.         //}
  36.         if (word[i] != ' ' && word[i] != ',' && word[i] != '\n' && word[i] != '.' && word[i] != '!' && word[i] != '?' && word[i] != ';' && word[i] != ':' && word[j] != '\0') {
  37.             continue;
  38.         }
  39.         else {
  40.             z = i - z;
  41.             if (word[i + 1] == ' ') i++;
  42.             temp = (char*)realloc(temp, sizeof(char) * z);
  43.             temp[z] = '\0';
  44.             int m = start;
  45.             for (int t = 0; t < z; t++) {
  46.                 temp[t] = word[m];
  47.                 m++;
  48.             }
  49.             for (k = 0; k < z; k++) {
  50.                 for (j = k; j < z; j++) {
  51.                     if (temp[j] == temp[k] && j != k)
  52.                         flag = 1;
  53.                 }
  54.                     if (k == z - 1 && flag == 0 && k > max) {
  55.                         max = k;
  56.                         int lent = mystrlen(temp);
  57.                         max_word = (char*)realloc(max_word, sizeof(char) * lent);
  58.                         temp[lent] = '\0';
  59.                         max_word = temp;
  60.                     }
  61.                 }
  62.             z = i + 1;
  63.             start = i + 1;
  64.             flag = 0;
  65.             }
  66.         }
  67.     printf("%s\n", max_word);
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement