0
#include<stdio.h>
void reverse(char s[], int i);
main()
{
    int i,c;                                            
    char s[1000];
    for(i=0;(c=getchar())!='\n';i++)
    {
        s[i]=c;
        c=getchar();
    }
    void reverse(s,i);               [B] //getting error in this line[/B]
    return 0;
}
void reverse(char input[], int n)
{
    int i;
    for(i=n;i>=0;--i)
    {
        printf("%c", input[i]);
    }
}

i am writing a program to reverse the input string, with the help of a function "reverse". However, my program doesn't compile. Pls tell me the cause of the error , as i have tried a lot to spot it but fails. I'm new to programming.

4
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by nathanurag
1

You don't need to put the return type of the function before its name when you are calling it (definitely need it in the prototype and in the definition though) reverse(s,i); is all you need on that line you have marked.

Also, you should put int in front of main (your compiler might be doing it implicitly but don't rely on that)

Edited by jonsca: n/a

2

working now. You dont need to mention the return type while calling a function.
Main is also a function. you have to mention its return type "void" or "int".

#include<stdio.h>
void reverse(char s[], int i);
int main()
{
    int i,c;                                            
    char s[1000];
    for(i=0;(c=getchar())!='\n';i++)
    {
        s[i]=c;
        c=getchar();
    }
    reverse(s,i);                //getting error in this line
    return 0;
}
void reverse(char input[], int n)
{
    int i;
    for(i=n;i>=0;--i)
    {
        printf("%c", input[i]);
    }
}
1

you have to mention its return type "void" or "int".

void main is actually incorrect (though your compiler may take it)
See this article.

0

Hi,

Just one correction in your program.

You doesn't need to call getchar() twice, in your case, you are skipping every next character read by using second getchar()(second getchar() read into 'c' is overwritten by first getchar()) in the for loop.

exact program will be:

#include<stdio.h>
void reverse(char s[], int i);
int main()
{
    int i,c;                                            
    char s[1000];
    for(i=0;(c=getchar())!='\n';i++)
    {
        s[i]=c;
    }
    reverse(s,i);                //getting error in this line
    return 0;
}
void reverse(char input[], int n)
{
    int i;
    for(i=n;i>=0;--i)
    {
        printf("%c", input[i]);
    }
}

Regards,
Gplkrsna.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.