简体   繁体   中英

Reverse string from file, replace the characters in the string with another string and print it

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';
}

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, '%');

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM