Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #include <stdbool.h>
  6. int get_line (char *buff, size_t sz) {
  7.     int ch, extra;
  8.    
  9.     if (fgets (buff, sz, stdin) == NULL)
  10.         return -1;
  11.    
  12.     // If it was too long, there'll be no newline. In that case, we flush
  13.     // to end of line so that excess doesn't affect the next call.
  14.     if (buff[strlen(buff)-1] != '\n') {
  15.         extra = 0;
  16.         while (((ch = getchar()) != '\n') && (ch != EOF))
  17.             extra = 1;
  18.         return (extra == 1) ? -2 : 0;
  19.     }
  20.    
  21.     // Otherwise remove newline and give string back to caller.
  22.     buff[strlen(buff)-1] = '\0';
  23.     return 0;
  24. }
  25.  
  26. int check_string( char s[] ) //функция проверки строка на символы
  27. {
  28.     unsigned char c; //здесь будем хранить текущий проверямый символ
  29.    
  30.     while ( ( c = *s ) && ( isalpha( c ) || isspace( c ) ) ) { //цикл по всем буквам, присваеваем указатель на букву переменной с, вызываем встроенные функции isaplha и isspace, они проверяют является символ буквой и пробелом
  31.         //если встретится символ небуква и непробел, то цикл заканчивается
  32.         ++s; //увеличиваем указатель чтобы переместить на следующую букву
  33.     }
  34.    
  35.     return *s == '\0'; //если в цикле дошли до конца, то это сравнение вернет true, если нет, то false
  36. }
  37.  
  38. int main() {
  39.     printf("Enter the sentence: ");
  40.     char str[512];
  41.     int rc = get_line(str, sizeof(str)); //ввод строки от пользователя, в rc результат работы, 0 - строка прочитана, -1 - ничего не было введено, -2 - слоишком длинная строка
  42.     if (rc == -1) //здесь проверяем результат
  43.     {
  44.         printf("no input");
  45.         return 1;
  46.     } else if (rc == -2) { //здесь тоже
  47.         printf("input string too long");
  48.     }
  49.     //ниже если строка хорошая, то проверяем ее на наличие символов отличных от букв и пробела
  50.     if (check_string(str)) {
  51.         printf("sentence doesn't contain anything else letters and spaces");
  52.     }   else {
  53.         printf("sentence contain anything else letters and spaces");
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement