Advertisement
Mixilino

najkraci najduzi podniz cifri FINAL

Apr 20th, 2019
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.74 KB | None | 0 0
  1. int daLiJeCifra(char str);
  2. void najkraciPodstringCifri(char str1[], char str2[]);
  3. void najduziPodstringCifri(char str1[], char str2[]);
  4.  
  5. int main(void) {
  6.     printf("Najkraci najduzi podstring od cifri:\n");
  7.     char str1[50] = "svdb638sh22h1 ";
  8.     char str2[50], str3[50];
  9.     najkraciPodstringCifri(str1, str2);
  10.     printf("Najkraci podstring cifri \"%s\" od stringa \"%s\"\n\n", str2, str1);
  11.     najduziPodstringCifri(str1, str3);
  12.     printf("Najduzi podstring cifri \"%s\" od stringa \"%s\"\n\n", str3, str1);
  13.     return 0;
  14. }
  15. int daLiJeCifra(char str) {
  16.     if (str >= '0' && str <= '9') {
  17.         return 1;
  18.     }
  19.     return 0;
  20. }
  21. void najkraciPodstringCifri(char str1[], char str2[]) {
  22.     int i = 0, j = 0, max = 0, duzina, index, temp = 0;
  23.     while (str1[i] != '\0') {
  24.         j = i;
  25.         if (daLiJeCifra(str1[i])) {
  26.             while (daLiJeCifra(str1[j]) && str1[j] != '\0') {
  27.                 j++;
  28.             }
  29.         }
  30.         duzina = j - i;
  31.         if (temp == 1) {
  32.             if (duzina < max && duzina != 0) {
  33.                 index = i;
  34.                 max = duzina;
  35.             }
  36.         }
  37.         else if (temp == 0 && duzina > 0) {
  38.             index = i;
  39.             max = duzina;
  40.             temp++;
  41.         }
  42.         i = j + 1;
  43.     }
  44.     for (int i = index; i < max + index; i++)
  45.     {
  46.         str2[i - index] = str1[i];
  47.     }
  48.     str2[max] = '\0';
  49. }
  50.  
  51. void najduziPodstringCifri(char str1[], char str2[]) {
  52.     int i = 0, j, max, index, duzina, temp = 0;
  53.     while (str1[i] != '\0') {
  54.         j = i;
  55.         if (daLiJeCifra(str1[i])) {
  56.             while (daLiJeCifra(str1[j]) && str1[j] != '\0') {
  57.                 j++;
  58.             }
  59.         }
  60.         duzina = j - i;
  61.         if (temp) {
  62.             if (duzina != 0 && max < duzina) {
  63.                 max = duzina;
  64.                 index = i;
  65.             }
  66.         }
  67.         else if (temp == 0 && duzina != 0) {
  68.             max = duzina;
  69.             index = i;
  70.             temp++;
  71.         }
  72.         i = j + 1;
  73.     }
  74.     for (int i = index; i < max + index; i++)
  75.     {
  76.         str2[i - index] = str1[i];
  77.     }
  78.     str2[max] = '\0';
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement