繁体   English   中英

如何限制/停止 fgets 读取的字符数?

[英]How can I limit/stop the number of characters fgets reads?

我需要我的代码只读取 10 个字符的单词,而不再读取。 代码的目的是检测字谜。 如何将 fgets 限制为仅读取 10 个字符,然后显示一条消息 printf(“Word 超过 10 个字符,请重试”)。 我的代码在下面,不胜感激。

 #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdint.h>
    
    // Sorting function to sort words
    
    void lowercase(char *str);
    void removespaces(char *str);
    int lol(char a[], char b[], int *_lenword1, int *_lenword2);
    
    int main(void){ 
    
    //Initiating two strings with a maximum limit fo 10 characters
     char FirstWord[12];
     char SecondWord[12];
    
    /* Console requests input of the first word, fgets reads word of unknown legnth without reading more than buffer allows
       Index begins at 0 to use strlen excess allocated characters can be replaced with /0                                      */
        printf("Please enter the first word: \n");
        fgets(FirstWord, 12,stdin);
        FirstWord[strlen(FirstWord)-1]='\0';
      
        printf("Please enter the second word: \n");
        fgets(SecondWord, 12, stdin);
        SecondWord[strlen(SecondWord)-1]='\0';
    
        int lenword1 = strlen(FirstWord); 
        int lenword2 = strlen(SecondWord);
        
     //Relaying strings to functions to convert all characters to lowercase   
       lowercase(FirstWord);
       lowercase(SecondWord);
    // Relaying strings to removespaces function so spaces in string are removed
       removespaces(FirstWord);
       removespaces(SecondWord);
    //Decisions 
       if (lol(FirstWord, SecondWord, &lenword1, &lenword2))
        printf("%s and %s are anagrams.\n",FirstWord, SecondWord);
      else
        printf("%s and %s are not anagrams.\n", FirstWord, SecondWord);
    
      return 0;
    }
    
    //Function to sort characters
    int lol(char a[], char b[], int *_lenword1, int *_lenword2){
    if (*_lenword1 == *_lenword2){
    int first[26] = {0}, second[26] = {0}, c=0;
    
      // Calculating frequency of characters of the first string
    
      while (a[c] != '\0') {
        first[a[c]-'a']++;
        c++;
      }
    
      c = 0;
    
      while (b[c] != '\0') {
        second[b[c]-'a']++;
        c++;
      }
    
      // Comparing the frequency of characters
    
      for (c = 0; c < 26; c++){
        if (first[c] != second[c]){
          return 0;
        }
      }
      return 1;
    }
    else{ // if different lengths - then they're not anagrams
            return 0; 
        }
    }
    // Function to convert all letters to lower case (to allow for case sensitivy anagram detection)
    void lowercase(char* str){
    for (uint8_t i=0; str[i]; i++ ){
       str[i]=tolower(str[i]);
    }
    }
    // Function to remove all spaces from a given string 
    void removespaces( char* str) 
    { 
       const char* d = str;
        do {
            while (*d == ' ') {
                ++d;
            }
        } while (*str++ == *d++);
    }

像这样的东西:

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

int main()
{
    char v[12];
    
    //Loop checks if fgets succeeded and if there is '\n' in the string:
    while (!fgets(v, 12, stdin) || strcspn(v, "\n") == 11){
        if(feof(stdin)) {
             printf("Error: End of file reached.\n");
             exit(EXIT_FAILURE);
        }
        printf("Error: The word is longer than 10 characters, try again.\n");
        for(int ch=getchar(); ch != '\n' && ch != EOF; ch=getchar());
    }
    v[strcspn(v, "\n")] = '\0';
    
    printf("%s\n", v);

    return 0;
}

如果字符串中没有“\n”,则该单词的字符数超过 10 个。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM