0

I am tasked with creating a program that uses functions to take a user input word, store it in an array, and then display this word, the word in reverse, the word vertically, then the word reverse and vertical. For example:

Input:

array

Output:

array
 ----
yarra
 ---- 
a
r
r
a
y
------
y
a
r
r
a

So far, I have the code to store and display the word, I am just wondering what the best way to display this in reverse would be? The code so far:

#include <stdio.h>

void primaryFunction()
{
  char arrA[100] = {0};

  int n;
    //unsigned char ch;
    unsigned int i = 0;
    printf("Enter Your Word:\n");

    for(i = 0; i<n; i++)
    {
    scanf("%c", &arrA[i]);
    printf("\n%c",arrA[i]); 
    printf("%c",arrA[i]);           
    }
}

int main(void)
{
  primaryFunction();
}

Any guidance can help. Thank you!!

ikegami
  • 367,544
  • 15
  • 269
  • 518

4 Answers4

0

Let's assume you already have the word in arrA. Then to display in reverse order just do:

int len = strlen(arrA);
for (int i = len - 1; i >= 0; i--)
{
    printf("%c", arrA[i]);
}

And to display per line in reverse order each letter do:

printf("%s", "\n");
for (int i = len - 1; i >= 0; i--)
{
    printf("%c\n", arrA[i]);
}

The full code would look like this:

int main()
{
    char arrA[100];

    printf("Enter the word: ");
    scanf("%s", arrA);

    //Prints the word
    printf("%s\n----\n", arrA);

    int len = strlen(arrA);

    //Prints in reversed order in a line
    for (int i = len - 1; i >= 0; i--)
    {
        printf("%c", arrA[i]);
    }
    printf("%s", "\n----\n");

    //Prints in order in multiple lines
    for (int i = 0; i < len; i++)
    {
        printf("%c\n", arrA[i]);        
    }

    //Prints in reverse order in multiple lines.
    printf("%s", "----\n");
    for (int i = len - 1; i >= 0; i--)
    {
        printf("%c\n", arrA[i]);
    }

    return 0;
}
Javier Silva Ortíz
  • 2,864
  • 1
  • 12
  • 21
0

This problem is not so difficult, so we can solve it in a straightforward way.

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

#define MAXLEN 100

void primaryFunction() {
    char arrA[MAXLEN] = {0};
    char Arra[MAXLEN] = {0};
    int i, len = 0;

    printf("Enter Your Word:\n");
    scanf("%s", arrA);               // store the input word
    printf("%s\n----\n", arrA);

    len = strlen(arrA);
    for (i = 0; i < len; ++i) {
        Arra[i] = arrA[len - i - 1]; // reverse it!
    }
    printf("%s\n----\n", Arra);

    for (i = 0; i < len; ++i) {
        printf("%c\n", arrA[i]);
    }

    printf("----\n");
    for (i = 0; i < len; ++i) {
        printf("%c\n", Arra[i]);
    }
}

int main() {
    primaryFunction();
}

input

array

output

array
----
yarra
----
a
r
r
a
y
----
y
a
r
r
a
taseikyo
  • 126
  • 7
0
#include <stdlib.h>
#include <stdio.h>

#define MAXLEN 10

void primaryFunction()
{
    char arrA[MAXLEN] = {0};

    printf("Enter Your Word:\n");
    int n = 0;
    int e = 0;

    //Use this just in case you run out of memory
    while (e != 10 && n < MAXLEN) {
        scanf("%c", &arrA[n]);
        e = (int)arrA[n];
        n++;
    }

    n--;

    for(int i = 0; i<n; i++)
    {
        printf("%c", arrA[i]);           
    }

    printf("\n----\n");

    for(int i = (n-1); i>=0; i--)
    {
        printf("%c", arrA[i]);           
    }

    printf("\n----\n");

    for(int i = 0; i<n; i++)
    {
        printf("%c\n", arrA[i]);           
    }

    printf("----\n");

    for(int i = (n-1); i>=0; i--)
    {
        printf("%c\n", arrA[i]);           
    }
}

int main(void)
{
  primaryFunction();
}
ghost_duke
  • 121
  • 7
-1

You can just reverse the string this way and print

for(int i=0, j=strlen(string)-1; i<j; ++i, --j) swap(string[i], string[j]);

or even print the string in reverse order.

for(int i=strlen(string)-1; i>=0; --i) printf("%c\n", string[i]);