Read symbols from the keyboard and replace them with a specific pathterin in C



  • We need to scrub the line that the user turns from the keyboard and change it on a certain pathet, and then put it in the console. Can't check the condition that in the current symbol only the letters how to finish or improve the code? введите сюда описание изображения

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    

    int checkString(const char s[]) {
    unsigned char c;
    while ( (c = *s) && (isalpha(c) || isblank(c))) ++s;
    return *s == '\0';
    }

    void replacechar(char *s, char c1, char c2) {
    int i = 0;
    for(i = 0; s[i]; i++) {
    if(s[i] == c1) {
    s[i] = c2;
    }
    }
    }

    void main() {
    int a;
    unsigned count = 0;
    printf("Enter your number: ");
    while ((a = getchar()) != '~') {
    // If that was in first time - first symbol is dot
    if ((char) a == '.' && count == 0) printf("0.");
    // If second symbol is letter than stop
    if (isalpha((char) a) == 1) printf("%c", (char) a);
    }
    }



  • I would do something to meet all the the requirements (and the introduction and the withdrawal and the occasional entry):

    int main()
    {
        for(;;)
        {
            char buf[256] = { 0 }, *o = buf;
            printf("Введите число: ");
            int c = getchar();
            if (c == '\n') break;      // Пустая строка - выход
            if (c == '.' || c == ',')  // Точка первая или нет?
                *o++ = '0';
            ungetc(c,stdin);
    
        int dot = 0;
        for(;;)
        {
            c = getchar();
            if (isdigit(c)) { *o++ = c; continue; } // Цифра
            if (!dot &amp;&amp; (c == '.' || c == ','))     // Точка,
            {                          // которой еще не было
                *o++ = '.';
                dot = 1;
            } else break;
        }
        while(c != '\n') c = getchar();
        printf("Вы ввели: %s", buf);
        if (!dot &amp;&amp; o != buf) printf(".00");  // Если точки не было,
        puts("");                             // а число было
    }
    

    }

    The end of the job is when the line is empty.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2