Вы находитесь на странице: 1из 5

По теме: ДИНАМИЧЕСКОЕ ВЫДЕЛЕНИЕ ПАМЯТИ

Лабораторная Nr.5
6 вариант

Сделал : группа AI-212 Мельник Роман

Проверил: Максим Игнатов


Задание:

Вариант 6. Отсортировать слова в алфавитном порядке для заданного предложения.

#include <stdio.h>

#include <string.h>

int main() {

// array with original sentence

char str[200] = "";

// array that will hold words

char m[200][200];

// temp variable for swap

char s[200];

printf("Type sentence: ");

fgets(str, 200, stdin);

// get first word

char * token = strtok(str, " \t,.;\n");

int count = 0;

// loop through all the words

while( token != NULL ) {

strcpy(m[count], token);

count++;

token = strtok(NULL, " \t,.;\n");

}
printf("Original words: \n");

for(int i = 0; i < count; i++) {

printf("%s\n", m[i]);

// sort words in alphabetical order

for(int i = 0; i < count; i++) {

for(int j = i+1; j < count; j++)

// create temp variables and uppr them, to compare strings

char tempOne[200];

strcpy(tempOne, m[i]);

char tempTwo[200];

strcpy(tempTwo, m[j]);

strupr(tempOne);

strupr(tempTwo);

// swap words, if first one is bigger than other one (bigger means it is placed
further in alphabet)

if(strcmp(tempOne, tempTwo) > 0){

strcpy(s,m[i]);

strcpy(m[i],m[j]);

strcpy(m[j],s);

}
}

printf("\nSorted words: \n");

for(int i = 0; i < count; i++) {

printf("%s\n", m[i]);

return 0;

Вам также может понравиться