Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int convertString(char s[]);
- bool isLetter(char s);
- void swapWords(char a[], char b[]);
- int length(char s[]);
- void coppy(char a[], char b[]);
- bool isSmaller(char a[], char b[]);
- int main()
- {
- /*
- FILE* fs;
- fs = fopen("in.txt", "r");
- char string[11];
- int sum = 0;
- while(fgets(string, 11, fs))
- sum += convertString(string);
- fclose(fs);
- printf("Suma este egala cu %d.", sum);*/
- char string[100], words[10][100], j=0, nrOfWords=0;
- scanf("%[^\n]", string);
- for (int i = 0; string[i]; i++)
- {
- if (isLetter(string[i]))
- {
- words[nrOfWords][j] = string[i];
- j++;
- }
- else
- {
- words[nrOfWords][j] = 0;
- nrOfWords++;
- j = 0;
- }
- }
- words[nrOfWords][j] = 0;
- for (int i = 0; i < nrOfWords; i++)
- {
- for (int j = i + 1; j <= nrOfWords; j++)
- if (length(words[i]) < length(words[j]))
- swapWords(words[i], words[j]);
- else
- if (length(words[i]) == length(words[j]) && isSmaller(words[i], words[j]))
- swapWords(words[i], words[j]);
- }
- for (int i = 0; i <= nrOfWords; i++)
- printf("%s\n", words[i]);
- return 0;
- }
- int convertString(char s[]) ///123
- {
- int nr = 0;
- for (int i = 0; s[i]; i++)
- if (s[i] >= '0' && s[i] <= '9')
- nr = nr * 10 + (s[i] - '0');
- return nr;
- }
- bool isLetter(char s)
- {
- return (s >= 'a' && s <= 'z') || (s >= 'A' && s <= 'Z');
- }
- int length(char s[])
- {
- int nr = 0;
- for (int i = 0; s[i]; i++)
- nr++;
- return nr;
- }
- void swapWords(char a[], char b[])
- {
- char aux[100];
- coppy(aux, a);
- coppy(a, b);
- coppy(b, aux);
- }
- void coppy(char a[], char b[])
- {
- int i;
- for (i = 0; b[i]; i++)
- a[i] = b[i];
- a[i] = 0;
- }
- bool isSmaller(char a[], char b[])
- {
- for (int i = 0, j = 0; a[i] && b[i]; i++, j++)
- if (a[i] < b[i])
- return 1;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement