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

typedef char String[80];

int main() {
    String originalStr = "mail";
    String reversedStr; 
    int index;
    int len = strlen(originalStr);

    for (index = 0; index < len; index++) { 
        reversedStr[index] = originalStr[len - index -1];
    }
    reversedStr[index] = '\0'; 

    printf("Original is %s, reversed is %s\n", originalStr, reversedStr);
}

I am wondering how can I modify the main function to use a void function with the prototype: void reverseString(String originalStr, String reversedStr);

As a matter of fact, I cannot understand difference between void and main.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Rodi
  • 1
  • 4
    You need to check [The Definitive C Book Guide and List](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and find a good beginners book or tutorial. It will tell you how to create functions (which you actually know, `main` is just a normal function). Then continue on [how to ask good questions](http://stackoverflow.com/help/how-to-ask). And finally update your question to be more specific with your problem, what exactly is the problem you are having? How to create functions? (Read a book.) Understand what `void` means in different places? (Also read a book.) – Some programmer dude Mar 19 '16 at 12:14
  • Also, you should remove C++ tag. – Martin James Mar 19 '16 at 12:16
  • There are [many similar questions](http://stackoverflow.com/q/204476/1460794). – wally Mar 19 '16 at 12:21
  • The **main** function is the main function of the program, what you want to do is to define another function, and this function will be called from the main function. You should read some tutorial first, because obviously your not very clear with what you are doing – xxxmatko Mar 19 '16 at 12:26

1 Answers1

2

Before you go any deeper into the C language, you must learn to indent your code correctly. It will make your code readable and prevent many stupid mistakes.

I reformatted your posted code, look at how it is done.

Regarding your question, you are supposed to move the code for reversing the string into a separate function for which you are given the prototype:

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

typedef char String[80];

void reverseString(String originalStr, String reversedStr) {
    int index;
    int len = strlen(originalStr);

    for (index = 0; index < len; index++) { 
        reversedStr[index] = originalStr[len - index - 1];
    }
    reversedStr[index] = '\0'; 
}         

int main(void) {
    String originalStr = "mail";
    String reversedStr; 
    reverseString(originalStr, reversedStr);
    printf("Original is %s, reversed is %s\n", originalStr, reversedStr);
}

Functions are the basic block in programming languages to break down functionality into smaller reusable steps. Note that originalStr and reverseStr in the function are the names of function arguments. They are local to the function. The values passed from main happen to have the same name by construction, but this is not necessary. Learn more from a good tutorial book.

chqrlie
  • 131,814
  • 10
  • 121
  • 189