Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <stdbool.h>
- int get_line (char *buff, size_t sz) {
- int ch, extra;
- if (fgets (buff, sz, stdin) == NULL)
- return -1;
- // If it was too long, there'll be no newline. In that case, we flush
- // to end of line so that excess doesn't affect the next call.
- if (buff[strlen(buff)-1] != '\n') {
- extra = 0;
- while (((ch = getchar()) != '\n') && (ch != EOF))
- extra = 1;
- return (extra == 1) ? -2 : 0;
- }
- // Otherwise remove newline and give string back to caller.
- buff[strlen(buff)-1] = '\0';
- return 0;
- }
- int check_string( char s[] ) //функция проверки строка на символы
- {
- unsigned char c; //здесь будем хранить текущий проверямый символ
- while ( ( c = *s ) && ( isalpha( c ) || isspace( c ) ) ) { //цикл по всем буквам, присваеваем указатель на букву переменной с, вызываем встроенные функции isaplha и isspace, они проверяют является символ буквой и пробелом
- //если встретится символ небуква и непробел, то цикл заканчивается
- ++s; //увеличиваем указатель чтобы переместить на следующую букву
- }
- return *s == '\0'; //если в цикле дошли до конца, то это сравнение вернет true, если нет, то false
- }
- int main() {
- printf("Enter the sentence: ");
- char str[512];
- int rc = get_line(str, sizeof(str)); //ввод строки от пользователя, в rc результат работы, 0 - строка прочитана, -1 - ничего не было введено, -2 - слоишком длинная строка
- if (rc == -1) //здесь проверяем результат
- {
- printf("no input");
- return 1;
- } else if (rc == -2) { //здесь тоже
- printf("input string too long");
- }
- //ниже если строка хорошая, то проверяем ее на наличие символов отличных от букв и пробела
- if (check_string(str)) {
- printf("sentence doesn't contain anything else letters and spaces");
- } else {
- printf("sentence contain anything else letters and spaces");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement