Создать программы для решения задачи по своему варианту.
С клавиатуры вводится текстовая строчка. Разработать программу, реализующую указанные действия.
1.а) выводит все символы, расположенные после первого символа "!"; б) подсчитывает количество разных слов, входящих в заданный текст.
C/C++
Нужен код на С :)
б)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#define BUFFER 0xFFF
typedef struct {
char key[32];
size_t value;
} Pair;
size_t emplace_back(const char* key, Pair* map, size_t length) {
size_t i = 0, n;
while (i < length) {
if (!strcmp(map[i].key, key)) {
++map[i].value;
return length;
}
++i;
}
strcpy_s(map[i].key, _countof(map[i].key), key);
map[i].value = 1;
n = length + 1;
assert(n < BUFFER);
return n;
}
void show(Pair* map, size_t length) {
size_t i = 0;
while (i < length) {
printf("%s: %u\n", map[i].key, map[i].value);
++i;
}
}
int main(void) {
size_t length = 0;
Pair map[BUFFER];
char* word = NULL;
char* context = NULL;
const char* delim = " ,.!:;";
char text[BUFFER];
char line[BUFFER];
printf("String: ");
fgets(text, BUFFER, stdin);
text[strlen(text) - 1] = 0;
strcpy_s(line, BUFFER, text);
word = strtok_s(line, delim, &context);
while (word != NULL) {
length = emplace_back(word, map, length);
word = strtok_s(NULL, delim, &context);
}
puts("\tResult:");
show(map, length);
system("pause > nul");
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#define BUFFER 0xFFF
typedef struct {
char key[32];
size_t value;
} Pair;
size_t emplace_back(const char* key, Pair* map, size_t length) {
size_t i = 0, n;
while (i < length) {
if (!strcmp(map[i].key, key)) {
++map[i].value;
return length;
}
++i;
}
strcpy_s(map[i].key, _countof(map[i].key), key);
map[i].value = 1;
n = length + 1;
assert(n < BUFFER);
return n;
}
void show(Pair* map, size_t length) {
size_t i = 0;
while (i < length) {
printf("%s: %u\n", map[i].key, map[i].value);
++i;
}
}
int main(void) {
size_t length = 0;
Pair map[BUFFER];
char* word = NULL;
char* context = NULL;
const char* delim = " ,.!:;";
char text[BUFFER];
char line[BUFFER];
printf("String: ");
fgets(text, BUFFER, stdin);
text[strlen(text) - 1] = 0;
strcpy_s(line, BUFFER, text);
word = strtok_s(line, delim, &context);
while (word != NULL) {
length = emplace_back(word, map, length);
word = strtok_s(NULL, delim, &context);
}
puts("\tResult:");
show(map, length);
system("pause > nul");
return 0;
}
#include <stdio.h>
#include <string.h>
const char abc[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int is_bukva(const char c)
{
for(int x = 0; x < strlen(abc); x++)
if(c == abc[x]) return 1;
return 0;
}
int main()
{
const int size = 100;
char s[size];
fgets(s,size,stdin);
printf("Все символы после !\n");
int find_simbol = 0;
for(int x = 0; x < strlen(s); x++)
{
if(find_simbol) putchar(s[x]);
else if(s[x] == '!') find_simbol = 1;
}
printf("\n\n");
int count_words = 0;
for(int x = 0; x < strlen(s); x++)
{
if(x > 0)
{
if( is_bukva(s[x-1]) && ! is_bukva(s[x]) ) count_words++;
else if( ( x == strlen(s) - 1 ) && is_bukva(s[x]) ) count_words++;
}
}
printf("Количество слов -- %d.\n", count_words);
return 0;
}
#include <string.h>
const char abc[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int is_bukva(const char c)
{
for(int x = 0; x < strlen(abc); x++)
if(c == abc[x]) return 1;
return 0;
}
int main()
{
const int size = 100;
char s[size];
fgets(s,size,stdin);
printf("Все символы после !\n");
int find_simbol = 0;
for(int x = 0; x < strlen(s); x++)
{
if(find_simbol) putchar(s[x]);
else if(s[x] == '!') find_simbol = 1;
}
printf("\n\n");
int count_words = 0;
for(int x = 0; x < strlen(s); x++)
{
if(x > 0)
{
if( is_bukva(s[x-1]) && ! is_bukva(s[x]) ) count_words++;
else if( ( x == strlen(s) - 1 ) && is_bukva(s[x]) ) count_words++;
}
}
printf("Количество слов -- %d.\n", count_words);
return 0;
}
Cengiz Han
Пункт б не совсем понял. Я сделал вывод количества всех слов в тексте. Или нужно было вывести количество слов после восклицательного знака?
Похожие вопросы
- Очень нужен код на c++ (тема: ГРАФЫ)
- Очень нужна помощь. нужен код на C. Задачка будет в описании.
- Программа на с++ (нужен код) пожалуйста помогите
- Задание с использование библиотечных функций С++. Нужен код. Помогите, пожалуйста.
- Нужен код на С. Подробней в описании
- Нужен код на Си помогите пж
- Функция на рекурсию. Нужен код на С
- Нужен код на Си помогите пожалуйста
- Нужен код на Си помогите пж
- Нужен код на С++