Problrm in invesing a char array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alenik1989
    New Member
    • Oct 2007
    • 64

    Problrm in invesing a char array

    i have this code which suppose to get a n input from the file and store it in a char array then i have to inverse it.....
    rhis code isnt complete but im getting a wiered error
    i see the error when the c++ starts linking not in the compilation procces.
    my code is
    [CODE=cpp]
    #include<iostre am>
    #include<cstrin g>
    #include<fstrea m>
    int reverse(char* str);
    using namespace std;

    int main()
    {
    ifstream infile;
    ofstream outfile;
    infile.open("in put.txt");
    outfile.open("o utput.txt");
    char name[60];
    char invname[60];

    if(infile.fail( ))
    cout<<"FAILED";
    else
    {
    while(!infile.e of())
    {
    infile.getline( name,'\n');
    reverse(name);
    }
    }
    return 0;
    }


    int Reverse(char* str)
    {
    if (NULL==str)retu rn -1; //no string
    int l=strlen(str)-1; //get the string length
    if (1==l)return 1;

    for(int x=0;x < l;x++,l--)
    {
    str[x]^=str[l]; //triple XOR Trick
    str[l]^=str[x]; //for not using a temp
    str[x]^=str[l];
    }
    return 0;
    }

    [/CODE]
    the error is
    [CODE=txt]
    Linking...
    asdqwe.obj : error LNK2001: unresolved external symbol "int __cdecl reverse(char *)" (?reverse@@YAHP AD@Z)
    Debug/asdqwe.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    asdqwe.exe - 2 error(s), 0 warning(s)
    [/CODE]

    can enyone explain the error for me please and tell what to do......ASAP please!!!!!!
  • mschenkelberg
    New Member
    • Jun 2007
    • 44

    #2
    Originally posted by Alenik1989
    i have this code which suppose to get a n input from the file and store it in a char array then i have to inverse it.....
    rhis code isnt complete but im getting a wiered error
    i see the error when the c++ starts linking not in the compilation procces.
    my code is
    [CODE=cpp]
    #include<iostre am>
    #include<cstrin g>
    #include<fstrea m>
    int reverse(char* str);
    using namespace std;

    int main()
    {
    ifstream infile;
    ofstream outfile;
    infile.open("in put.txt");
    outfile.open("o utput.txt");
    char name[60];
    char invname[60];

    if(infile.fail( ))
    cout<<"FAILED";
    else
    {
    while(!infile.e of())
    {
    infile.getline( name,'\n');
    reverse(name);
    }
    }
    return 0;
    }


    int Reverse(char* str)
    {
    if (NULL==str)retu rn -1; //no string
    int l=strlen(str)-1; //get the string length
    if (1==l)return 1;

    for(int x=0;x < l;x++,l--)
    {
    str[x]^=str[l]; //triple XOR Trick
    str[l]^=str[x]; //for not using a temp
    str[x]^=str[l];
    }
    return 0;
    }

    [/CODE]
    the error is
    [CODE=txt]
    Linking...
    asdqwe.obj : error LNK2001: unresolved external symbol "int __cdecl reverse(char *)" (?reverse@@YAHP AD@Z)
    Debug/asdqwe.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    asdqwe.exe - 2 error(s), 0 warning(s)
    [/CODE]

    can enyone explain the error for me please and tell what to do......ASAP please!!!!!!


    declare function after using namespace std;

    Comment

    • Alenik1989
      New Member
      • Oct 2007
      • 64

      #3
      Originally posted by mschenkelberg
      declare function after using namespace std;
      Done that
      made no differance
      enything else?

      Comment

      • Alenik1989
        New Member
        • Oct 2007
        • 64

        #4
        ok i got it i revised the code to
        [CODE=cpp]
        #include<iostre am>
        #include<cstrin g>
        #include<fstrea m>
        void reverseit(char array[]);
        using namespace std;
        int main()
        {
        ifstream infile;

        infile.open("in put.txt");
        outfile.open("o utput.txt");
        char name[60];


        if(infile.fail( ))
        cout<<"FAILED";
        else
        {
        while(!infile.e of())
        {
        infile.getline( name,60);
        reverseit(name) ;

        }
        }
        return 0;
        }




        void reverseit(char array[])
        {
        char c; int len, mid=0;

        len = strlen(array);
        mid = (len-1)/2;

        for(int i=0,j=len-1; i <=mid; i++,j--)
        { c = array[i];
        array[i] = array[j];
        array[j] = c;
        }
        cout <<array<<endl ;
        }
        [/CODE]

        this is working perfect but i have another problem
        this code suppose to determine if the input is palindrome (original and reversed have to be the same)!!!!
        programs works perfectly for words like MADAM but not for phrases like
        madam, i'm adam
        which it reversed is mada m'i ,madam if you ignore the spaces and punctuation while comparing it will read like madamimmadam and the original would be madamimadam...

        my question is how can i ignore the punctuation and spaces when copmaring the original and reversed arrays? (can i use strcmp)!!!
        TANX

        Comment

        • Laharl
          Recognized Expert Contributor
          • Sep 2007
          • 849

          #5
          You can simply ignore the character if it fails a isalpha() test. isalpha can be found in <cctype>.

          Comment

          • Alenik1989
            New Member
            • Oct 2007
            • 64

            #6
            ok i will try that
            anyone knows whats wrong with this func
            [CODE=cpp]
            void copy(char array[],char copyarray[])
            {
            strcpy(copyarra y,array);
            int i=0;
            char c;

            while (copyarray[i])
            {
            c=copyarray[i];
            putchar (toupper(c));
            i++;
            }
            }
            [/CODE]
            it suppose to get the array and make all of its contents to upper charachters!!!!
            but it repeats the original one after the upper one like when array=like
            copyarray is shown as LIKElike

            any ideas????

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              I can see the putchar so that explains LIKE. Now what are you doing after this functon returns?? Maybe displaying the original array??

              Comment

              • happyjack27
                New Member
                • Dec 2007
                • 4

                #8
                Originally posted by Alenik1989
                ok i will try that
                anyone knows whats wrong with this func
                [CODE=cpp]
                void copy(char array[],char copyarray[])
                {
                strcpy(copyarra y,array);
                int i=0;
                char c;

                while (copyarray[i])
                {
                c=copyarray[i];
                putchar (toupper(c));
                i++;
                }
                }
                [/CODE]
                it suppose to get the array and make all of its contents to upper charachters!!!!
                but it repeats the original one after the upper one like when array=like
                copyarray is shown as LIKElike

                any ideas????

                you're making the problem overly complicated. you don't need to use strcpy or putchar. just write one character at a time to the destination array: the upper case version of the corresponding character in the source array. you want to set each character in the destination array to be the upper case version of the corresponding character in the source array, right? so write that.

                Comment

                Working...