Home > Enterprise >  I'm trying to make a function to take user input of a palindrome. Why is it after I take input
I'm trying to make a function to take user input of a palindrome. Why is it after I take input

Time:11-11

#include<stdio.h>
#include<string.h>
#define ROWS 6
#define LENGTH 30

int main (void)
{

    int wordcount[ROWS]; // Incraments word count. i.e. Word 1, Word 2, Word 3...
    char word[LENGTH]; // Stores users string

    for(int i = 1; i < ROWS; i  )
    {
        if(i == 1)
        {
            printf("Word %d: ", i);
            scanf("%d %s", &wordcount[i], &word[0]); // Input ends here. Prints out the rest of the array with "no" values.
            i  ;
        }
        if(i == 2)
        {
            printf("Word %d: ", i);
            scanf("%d %s", &wordcount[i], &word[1]);
            i  ;
        }
        if(i == 3)
        {
            printf("Word %d: ", i);
            scanf("%d %s", &wordcount[i], &word[2]);
            i  ;
        }
        if(i == 4)
        {
            printf("Word %d: ", i);
            scanf("%d %s", &wordcount[i], &word[3]);
            i  ;
        }
        if(i == 5)
        {
            printf("Word %d: ", i);
            scanf("%d %s", &wordcount[i], &word[4]);
        }
        break;

    }

return 0;

}

I've tried multiple loops, changing syntax and placement, but nothing makes sense to me anymore. I AM NOT ALLOWED TO USE POINTERS, GLOBAL VARIABLES, OR ANY OTHER LIB FUNCTIONS BESIDES scanf(), printf(), fgets(), or strlen(). I have to make multiple functions to get user input, reverse the string, and find out whether or not it's a palindrome... but I can't seem to get past part 1.

CodePudding user response:

A few issues ...

  1. Indexes should start from 0 and not 1
  2. The word array needs to be 2D (not 1D)
  3. That is, you want an array of words that has ROWS number of words and each word can be [up to] LENGTH characters.
  4. A simple loop can get all words without any if statements
  5. It's better to use fgets and strlen instead of scanf for input where you prompt the user

Here is the refactored code:

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

#define ROWS        5
#define LENGTH      100

int
main(void)
{

    // length of each word
    int wordcount[ROWS];

    // Stores users string
    char word[ROWS][LENGTH];

    for (int i = 0; i < ROWS; i  ) {
        // prompt the user
        printf("Word %d: ", i   1);
        fflush(stdout);

        // get the line with the word
        if (fgets(word[i],LENGTH,stdin) == NULL)
            break;

        // get the word length
        size_t len = strlen(word[i]);

        // strip newline
        if ((len > 0) && (word[i][len - 1] == '\n')) {
            word[i][len - 1] = 0;
            --len;
        }

        // save the length
        wordcount[i] = len;
    }

    // print the words
    for (int i = 0;  i < ROWS;    i)
        printf("Word %d is %d bytes: '%s'\n",i   1,wordcount[i],word[i]);

    return 0;
}

CodePudding user response:

I was able to reproduce this by not entered an integer and only the string. This means scanf() fails on the first and the subsequent attempts. Here is a simplified version of your program with error checking:

#include <stdio.h>
#define ROWS 6
#define LENGTH 30

int main (void) {
    int wordcount[ROWS];
    char word[LENGTH];
    for(int i = 0; i < ROWS; i  ) {
        printf("Word %d: ", i   1);
        int n = scanf(" %d %s", wordcount   i, word   i);
        if(n != 2) {
            printf("scanf failed\n");
            break;
        }
    }
}

and example run:

Word 1: 1 abcd
Word 2: cd
scanf failed

Not clear from the problem description what you are trying to do with word but it's probably incorrect as you write a string to position 0 of word, then the next word 2 at position 1 (overwriting part of word 1) etc. Do you actually mean count of words or count of letters in the word? For the latter use strlen().

  • Related