0

I have to read a file from a command line argument. The file has a string that the program reads and reverses. The reversed code is then transformed into a new string by changing the characters in that string by reading the characters one by one.

I'm getting these errors when I compile:

-conflicting types for 'reverse'

-previous implicit declaration of reverse was here -> this is when I call "reverse(string);"

-previous implicit declaration of append was here -> this is when I call append similar to reverse

Lastly I want "?" to turn into 3F% and I cant append % somehow. It gives out something weird.

EDIT: Thank you so much for the help guys. But, when I do "append(string,'%')" it starts printing several random characters and printing wrong stuff.

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

int main(int argc, char **argv)

{
    FILE *fp;
    char input;
    char string[100];
    int change = 1;
    int capA = 65;
    int capZ = 90;
    int lowera = 97;
    int lowerz = 122;
    int c;
fp = fopen(argv[1], "r");



   while ((c = fgetc(fp)) != EOF)
    {
        if(c == '_' || c == '?' || c == ':' || c == '/' || c == '&' || c == ' ')
        {
            if(c == '?'){
            append(string,'3');
            append(string,'F');
            append(string, "%%");
            }
            change = 0;
        }
        else if(c >= capA && c <= capZ)
        {
            append(string,tolower(c));
            change = 0;
        }
        else if(c >= lowera && c <= lowerz)
        {
            append(string,toupper(c));
            change = 0;
        }
        else if(c >= '?' && c <= '?')

        {
            append(string,c);
            change = 0;
        }
        else
        {
            append(string,c);
            change = 0;
        }
        }
    fclose(fp);
    reverse(string);
    printf(string);
}


void reverse(char *string)
{
    int length, c;
    char *begin, *end, temp;

    length = string_length(string);

    begin = string;
    end = string;

    for ( c = 0 ; c < ( length - 1 ) ; c++ )
        end++;

    for ( c = 0 ; c < length/2 ; c++ )
    {
        temp = *end;
        *end = *begin;
        *begin = temp;

        begin++;
        end--;
    }
}

int string_length(char *pointer)
{
    int c = 0;

    while( *(pointer+c) != '\0' )
        c++;

    return c;
}

void append(char* s, char c)
{
    int len = strlen(s);
    s[len] = c;
    s[len+1] = '\0';
}

3 Answers3

0

In C, you should not call a function until you have defined it, or at least defined its prototype. Otherwise, the compiler doesn't know what the type of the function is, and has to make a guess (the 'implicit declaration').

Try reversing the order of your functions, and it should compile.

About the other issue -- shouldn't this:

append(string, "%%");

be simply this?

append(string, '%');
Richard Walters
  • 1,464
  • 8
  • 16
  • Thank you so much for the help guys. But, when I do "append(string,'%')" it starts printing several random characters and printing wrong stuff. – user3543383 May 06 '14 at 01:42
0

Place your subroutines on top of the main function and change

append(string, "%%");

to

append(string, '%');

The compiler reads from top to bottom you can't call functions in main when it is not yet defined prior it or create a function prototype on top see http://opencbp.sourceforge.net/en_US.ISO8859-1/books/opencbook/func.prototypes.html

Mestica
  • 1,489
  • 4
  • 23
  • 33
0

So I'm getting these errors when I compile: -conflicting types for 'reverse' -previous implicit declaration of reverse was here -> this is when I call "reverse(string);" -previous implicit declaration of append was here -> this is when I call append similar to reverse

These errors happen because you're calling inside main functions that haven't been declared yet (reverse and append). To get rid of that, you should either put the function main at the end of the file, after all other functions declaration, or you should declare prototypes for the other functions before main. In this case, if you decide to use prototypes, here's an example:

// Includes

void reverse(char *string);
int string_length(char *pointer);
void append(char* s, char c);

int main(int argc, char **argv){
   // Rest of code...

Lastly I want "?" to turn into 3F% and I cant append % somehow. It gives out something weird.

Your append function accepts a character as the second argument, and you're trying to call it using a char* in

append(string, "%%");

To make it work, you should call it like this:

append(string, '%');

Also, check this out if you don't know the difference between single and double quotes.

Another thing, you're including string.h to use strlen(), but you also have a function to calculate string length. If you're allowed to use string.h, you can use the following functions:

Instead of append, you can use

char * strcat ( char * destination, const char * source );

Instead of string_length, you can use

size_t strlen ( const char * str );
Community
  • 1
  • 1
Daniel Carvalho
  • 473
  • 1
  • 5
  • 17
  • Thank you so much for the help. But, when I do "append(string,'%')" it starts printing several random characters and printing wrong stuff – user3543383 May 06 '14 at 01:46
  • What would be considered "wrong stuff"? Could you provide an input test case, and expected output? I've tested here, and it seems to work alright. – Daniel Carvalho May 06 '14 at 01:53
  • Input: "file.txt" -> inside is "ABcd?" -> Turns into (bunch of random numbers)91810560.00000003DCba file.txt is the commandline argument which is the file read. – user3543383 May 06 '14 at 01:56
  • Just one test case is enough, I can create a test file here. – Daniel Carvalho May 06 '14 at 01:57
  • I've just added the function prototypes before the main function, and changed the append(string, "%%") to append(string, '%'), as stated on the answer. – Daniel Carvalho May 06 '14 at 02:01
  • I'm sorry, I've also changed the printf(string) to printf("%s", string). – Daniel Carvalho May 06 '14 at 02:03
  • Yeah I have that and it still seems to not work. I really wonder why @_@ Sorry for the trouble. Edit: TY SO MUCH ^^ – user3543383 May 06 '14 at 02:04
  • You're welcome. If that solved your question, please do not forget to mark this as the accepted answer. Good luck with your program. – Daniel Carvalho May 06 '14 at 02:07
  • Theres one last thing I want to bug you about. How would the code look if I took in standard input instead? – user3543383 May 06 '14 at 02:20
  • I'm not sure I understood this correctly, but you could use, instead of using while((c = fgetc(fp)) != EOF), something along the lines of while((c = fgetc(stdin)) != INSERT_A_TERMINATOR_CHARACTER_THAT_IS_NOT_USED). – Daniel Carvalho May 06 '14 at 02:32