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

Recommended Answers

All 6 Replies

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)

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]);
    }
}

thx to the posts, the program worked out fine.

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

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

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.

hi, thx for ur help
i really appreciate it!!!!:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.