Thread: Reversing an array and printing it out

1. 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;
}
}
}```

2. 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.

3. Originally Posted by jimblumberg
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.

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.

4. 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.

5. 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. 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.