I need to my code to only read words of 10 characters and no longer. The purpose of the code is to detect anagrams. How can I limit fgets to only read 10 characters and after that display a message printf("Word is longer than 10 characters please try again"). My code is below I'd appreciate any help.
#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++);
}
Something like this:
#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;
}
If there isn't a '\n' in the string then the word has more than 10 characters.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.