CPallini
Следующий код показывает эту технику. Этого должно быть достаточно, чтобы вы смогли завершить его с помощью sort_sentence
функция.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
char * modify_sentence( const char * sentence, size_t length);
//TODO:
//char * sort_sentence(const char * sentence, size_t length);
int main()
{
char sentence[SIZE];
printf("Prepared by ABC\n");
//...
printf("Type in a sentence with no more than %d characters including space: \n", SIZE);
fgets(sentence, SIZE, stdin);
size_t len = strlen(sentence);
if ( !len ) return -1;
// handle the newline (if present)
if ( sentence[len-1] == '\n')
{
sentence[len-1] = '\0';
--len;
}
printf("Sentence length is %lu\n", len);
printf("The original sentence is %s\n", sentence);
char * modified_sentence = modify_sentence(sentence, len);
if ( modified_sentence)
{
printf("The modified sentence is %s\n", modified_sentence);
free(modified_sentence);
}
// TODO:
//char * sorted_sentence = sort_sentence(sentence, len);
//...
}
char * modify_sentence( const char * sentence, size_t length)
{
char * s = (char *) malloc(length);
if ( ! s ) return s;
strncpy( s, sentence, length);
// as before
int i, j, k;
char tmp;
for(i = 0, j = 0; j < length; j++)
{
if(!isalpha(s[j]) || (j == length - 1))
{
if(j < length - 1)
k = j - 1;
else
k = j;
while( i < k)
{
tmp = s[i];
s[i] = s[k];
s[k] = tmp;
i++;
k--;
}
i = j + 1;
}
}
return s;
}
//TODO:
//char * sort_sentence(const char * sentence, size_t length)
//{
//...
//}
[обновление]
Этот код содержит частичную реализацию
sort_sentence
функция.
То
нижний, верхний, цифровой и не буквенно-цифровой подстроки группируются в возвращаемом значении. Сортировка подстрок отсутствует (оставлено в качестве упражнения).
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
char * modify_sentence( const char * sentence, size_t length);
char * sort_sentence(const char * sentence, size_t length);
int main()
{
char sentence[SIZE];
printf("Prepared by ABC\n");
//...
printf("Type in a sentence with no more than %d characters including space: \n", SIZE);
fgets(sentence, SIZE, stdin);
size_t len = strlen(sentence);
if ( !len ) return -1;
// handle the newline (if present)
if ( sentence[len-1] == '\n')
{
sentence[len-1] = '\0';
--len;
}
printf("Sentence length is %lu\n", len);
printf("The original sentence is %s\n", sentence);
char * modified_sentence = modify_sentence(sentence, len);
if ( modified_sentence)
{
printf("The modified sentence is %s\n", modified_sentence);
free(modified_sentence);
}
char * sorted_sentence = sort_sentence(sentence, len);
if ( sorted_sentence )
{
printf("The sorted sentence is %s\n", sorted_sentence);
free(sorted_sentence);
}
}
char * modify_sentence( const char * sentence, size_t length)
{
char * s = (char *) malloc(length);
if ( ! s ) return s;
strncpy( s, sentence, length);
// as before
int i, j, k;
char tmp;
for(i = 0, j = 0; j < length; j++)
{
if(!isalpha(s[j]) || (j == length - 1))
{
if(j < length - 1)
k = j - 1;
else
k = j;
while( i < k)
{
tmp = s[i];
s[i] = s[k];
s[k] = tmp;
i++;
k--;
}
i = j + 1;
}
}
return s;
}
struct CharacterInfo
{
int beg; // start index
int end; // pass-the-end index
int (*fun)(); // discriminant function
};
int notisalnum(int c) { return (! isalnum(c)); }
char * sort_sentence(const char * sentence, size_t length)
{
struct CharacterInfo ci[] = // this array is used to keep track of the substrings
{
{ 0, 0, islower }, // start, pass-the-end, discriminant of lower characters
{ 0, 0, isupper }, // " " " upper "
{ 0, 0, isdigit }, // " " " digit "
{ 0, 0, notisalnum }// " " " not alphanumeric "
};
const int LEN = sizeof(ci)/sizeof(ci[0]);
char * s = (char *) malloc(length);
if ( ! s ) return s;
int n;
int index = 0;
for (n = 0; n < LEN; ++n)
{
int i;
for (i = 0; i<length; ++i)
{
ci[n].beg = ci[n].end = index;
if ( ci[n].fun(sentence[i]) )
{
s[index] = sentence[i];
++index;
++(ci[n].end);
}
}
}
// TODO: sort the substrings
return s;
}
[/обновление]
Member 13786151
Кпаллини, простите, что снова беспокою вас.
Я действительно пытаюсь понять код, который вы предоставили, но это действительно трудно для меня...
Я пытался несколько раз для sort_sentence, это было постоянно получать ошибку.
Могу я еще раз попросить вас о помощи? Спасибо.
Member 13786151
Я не знаю, как сделать sorted_sentence, который вы оставили для меня...
Мне очень жаль CPallini, я действительно глуп в программировании, я пытался вставить код, но получал все ошибки, сумма больше я не понимаю коды.
Надеюсь, CPallini вы сможете мне помочь...Спасибо.