Thread: Reversing an array and printing it out

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    15

    Reversing an array and printing it out

    I've found a "problem" online. Text goes:

    Write C program that enables input of number n, (1 ≤ n ≤ 7) which stands for day of the week. If entered number does not correspond to any day, print error message of choice and ask user to repeat input of number n. For initialization of names of days use array of pointers. Print day user inputted. Use only pointer notation. Check which day has most consonants and print that day reversed.
    I had no problem with first part, but the last is one that bothers me. If I initialize an array of 7 elements (days) how do I check for number of consonants? My idea was to make separate array of consonants with 5 elements and do something with it. But, I'm stuck. Any help is appreciated. So far, without first part, I have this:

    Code:
    for (i = 0; i < 7; i++) {
        counter = 0;
        for (j = 0; (j < 20) || *(vowels + j) != '\0'; j++) {
            if (*(*(days + i) + j) != *(vowels + j))
                counter++;
            if (counter > temp) {
                temp = counter;
                index = i;
            }
        }
    }
    Last edited by 574515; 03-30-2020 at 02:05 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,553
    I don't see where your code satisfies even one of the criteria in your problem statement. Perhaps you posted the incorrect code?

    Edit:
    I also don't see how the topic title relates to either the problem statement or the code supplied.
    Last edited by jimblumberg; 03-30-2020 at 02:33 PM.

  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    15
    Quote Originally Posted by jimblumberg View Post
    I don't see where your code satisfies even one of the criteria in your problem statement. Perhaps you posted the incorrect code?

    Edit:
    I also don't see how the topic title relates to either the problem statement or the code supplied.
    I quoted a problem. I said that I've done first part with input n and printing it out. I've just put "logic" that I was thinking so far about counting consonants in days. It's easier for me to make an array of 5 for vowels and check if the letter IS NOT a vowel, than to make array of 21 consonants. And yes, my title is off putting, I see that now. I'm sorry. The program consists of 3 functions, main, print_Day function and most_Consonants. I've done main and print_Day. Problem is with:

    Check which day has most consonants and print that day reversed.Check which day has most consonants and print that day reversed.
    As said before, I've put that piece of code, because I passed an array of days and initialized array of vowels, as well as "counter", "temp" and "index" variables. I've read in rules that you should post as little of code as possible.

    Thank you for your response.

    EDIT:

    I am sorry if I came off as rude and I will provide as detailed TD;DR as possible.

    TL;DR
    I have an array of 7 elements. Every element is a string. I need to find which string in that array has most consonants and then print that string out reversed. Code I've posted in my first post is only a loop that was intended to be used as a counter and which does not work (thus me asking for help). I can provide rest of the code, but that rest is tested and working. Prints out a string depending on inputted number. I've called most_Consonants function in print_Day function and passed that array. In most_Consonants I have vowels[5] initialized. Hope this helps, and once again thanks everyone for looking and / or responding.
    Last edited by 574515; 03-30-2020 at 03:34 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,998
    I suggest that you break down the problem into parts, each of which corresponds to a function. For example:
    Code:
    /* Returns the number of consonants in the string: */
    int count_consonants(const char *str);
    
    /* Reverses the string in-place: */
    void reverse_str(char *str);
    Implement count_consonants and test that it works correctly. Then implement reverse_str and test that it works correctly. With these two, you can then implement:
    Code:
    void print_reversed_day_with_most_consonants(const char *days[], int num_days)
    {
        /* ... */
    }
    Note that I use a const char* to store a day name in case you want to use string literals for the day names. This means that in order to call reverse_str, you need to copy the name of the day with the most consonants to a local array, and then reverse that copy then print it.

    I added a num_days parameter for flexibility, but of course you can assume that there are exactly 7 days (but this assumption should be documented as a constant, e.g., by defining a macro). If you have learnt about size_t, then you should change the type of num_days to size_t.
    Last edited by laserlight; 03-30-2020 at 07:49 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2020
    Posts
    15
    Thank you very much for your response. I will look into it. I'm still trying to advance, since my classes are going "slower" and I'm reading books about C and trying to find online "problems" to solve on my own, as well as ones from my class assignments. I saw lots of people using size_t in their codes and I will make sure to read about it, as well as put it in practice. Once again, thank you for helping me and thanks everyone for taking a time to look at my thread.

  6. #6
    Registered User
    Join Date
    Apr 2020
    Location
    Greater Philadelphia
    Posts
    26
    Here's my solution. It took me an embarrassing amount of time, not having used C for several years.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    int main()
      {
         char buf[16];
         char *d[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
         char *errmsg = "", *p;
         int i, ncon, maxcon=0, bestday=0;
         do
            {
              printf("%sEnter an integer in range 1-7: ", errmsg);
              fgets(buf, sizeof(buf), stdin);
              i=atoi(buf);
              /* error message never displays if first input is ok */
              errmsg="Invalid input, try again.\n";
            }
         while (i<1 || i>7);
         printf("Corresponding day is %s\n", d[i-1]);
         for (i=0; i<7; i++)
           {
              ncon=0;
              p=d[i];        
              while (*p)
                 {
                    if (strchr("AEIOU", toupper(*p)) == NULL)
                      ncon++;
                    p++;
                 }
              if (ncon > maxcon)
                 {
                    maxcon = ncon;
                    bestday = i;
                 }
            }
         strcpy(buf, d[bestday]);
         puts(strrev(buf));
         return(0);
      }
    Actually, no single day has the most consonants, as Wednesday and Thursday both have the same number.
    Last edited by Alogon; 04-10-2020 at 02:01 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reversing elements in an array
    By supermcbrownie in forum C Programming
    Replies: 5
    Last Post: 04-16-2014, 05:14 PM
  2. Reversing an array
    By YannB in forum C Programming
    Replies: 4
    Last Post: 01-28-2014, 04:58 PM
  3. Need help in reversing words in an Array
    By Justin Page in forum C Programming
    Replies: 8
    Last Post: 07-22-2012, 12:18 AM
  4. string array reversing...
    By cit in forum C Programming
    Replies: 4
    Last Post: 03-28-2002, 03:09 AM
  5. Reversing a Multi-D Array
    By Bradley33 in forum C++ Programming
    Replies: 1
    Last Post: 11-19-2001, 11:37 AM

Tags for this Thread