string inversion

This is a discussion on string inversion within the C++ Programming forums, part of the General Programming Boards category; is there any function which convert the char array into its invert starting from the end and then copy it ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    39

    string inversion

    is there any function which convert the char array into its invert starting from the end and then copy it to another char array?

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    strrev(); might work.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    why not make your own?
    Code:
    int length;
    char chars[6]="elloH", charz[1024];
    length = strlen(chars);
    strlen(charz) = length;
    for(int i; i < length; )
    {
          charz[i] = chars[length - i];
          i++;
    }
    cout<<charz;
    output:
    Hello
    this is NOT tested !
    Last edited by Denethor2000; 04-30-2002 at 11:22 AM.

  4. #4
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Since you arent incrementing i that would be an eternal loop.


    EDIT:: I also noticed that charz isnt an array so that would fail pretty badly

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    erion... before you try to correct me learn some c. thats not an eternal loop...

    btw i edited the char cause that part was wrong yeah

  6. #6
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    oops sry didnt see that you put the incrementation down there instead.
    No reason to get so upset

    EDIT:: Ehh??? Now it looks like it really wont work. Now you have an array of 0 chars in charz and chars isnt even an array anymore.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    lol oops. ill put it on 1024

  8. #8
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    --------------------Configuration: denethorsbane - Win32 Debug--------------------
    Compiling...
    test.cpp
    C:\Program\MSVC\MSDev98\Petzold\Cyclecreate\test.c pp(7) : error C2440: 'initializing' : cannot convert from 'char [6]' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    C:\Program\MSVC\MSDev98\Petzold\Cyclecreate\test.c pp(7) : error C2466: cannot allocate an array of constant size 0
    C:\Program\MSVC\MSDev98\Petzold\Cyclecreate\test.c pp(7) : error C2133: 'charz' : unknown size
    C:\Program\MSVC\MSDev98\Petzold\Cyclecreate\test.c pp(8) : error C2664: 'strlen' : cannot convert parameter 1 from 'char' to 'const char *'
    Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    C:\Program\MSVC\MSDev98\Petzold\Cyclecreate\test.c pp(9) : error C2106: '=' : left operand must be l-value
    C:\Program\MSVC\MSDev98\Petzold\Cyclecreate\test.c pp(12) : error C2109: subscript requires array or pointer type
    C:\Program\MSVC\MSDev98\Petzold\Cyclecreate\test.c pp(17) : warning C4508: 'main' : function should return a value; 'void' return type assumed
    Error executing cl.exe.

    test.obj - 6 error(s), 1 warning(s)

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    sorry im not at home i dont have a compiler at school so ur gonna have to edit it a bit k?

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    i#include <iostream.h>
    #include <string.h>
    
    int main()
    {
      int length;
      char chars[6]="elloH", charz[1024];
      length = strlen(chars);
    
      //strlen(charz) = length;
    
      //since i is incremented in the body of the for loop 
      //don't need to do it in the loop () part.  However you do need
      //to intialize i to some value before you start using
      //it.  
      for(int i = 0; i < length; )
      {
    
          charz[i] = chars[length - i];
          //the first time through i = 0 so length - i = length, so
          // chars[length - i] will be the null char at the end of chars,
          // so you would be putting the null char at element 0 of      
          //charz.  That's not good.  it should be chars[length - 1 - i] to 
          //keep all the elements in the correct positions.
         
          i++;
      }
    
       //now charz will hold Hello, but there is no null terminating 
       //char after the o.  This means charz is a char array but not a 
       //string.  To make it a string you have to put a null char at the
       //end of charz manually.
    
       //at the end of the above loop i = length, which is where the
       //null char should go.  so:
       charz[i] = '\0';
    
       //now you can do this:
      cout<<charz;
    
      return 0;
    }

  11. #11
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    this might not be std but i'll do it like this

    Code:
     char str1[12],str2[12];
     --
     --
     strcpy(str1,str2);
     strrev(str2);
     --
     --

  12. #12
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Then again...

    CString sTemp;
    sTemp = "Hello";
    sTemp.MakeReverse();

    That's kinda cheating... oh well!

  13. #13
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    Originally posted by elad
    Code:
    i#include <iostream.h>
    #include <string.h>
    
    int main()
    {
      int length;
      char chars[5]="elloH", charz[1024];
      length = strlen(chars);
    
      //strlen(charz) = length;
    
      //since i is incremented in the body of the for loop 
      //don't need to do it in the loop () part.  However you do need
      //to intialize i to some value before you start using
      //it.  
      for(int i = 0; i < length; )
      {
    
          charz[i] = chars[length - i];
          //the first time through i = 0 so length - i = length, so
          // chars[length - i] will be the null char at the end of chars,
          // so you would be putting the null char at element 0 of      
          //charz.  That's not good.  it should be chars[length - 1 - i] to 
          //keep all the elements in the correct positions.
         
          i++;
      }

    actually length - i would be H the first time, not null. Length is the length of the char. I is the number to decrement.

    so it goes
    Code:
           LENGTH - I = RESULT(LETTER)
                          5 - 0 = 5(H)
                          5 - 1 = 4(e)
                          5 - 2 = 3(l)
                          5 - 3 = 2(l)
                          5 - 4 = 1(o)
    LENGTH - I - 1 would make this:

    Code:
           LENGTH - I - 1 = RESULT(LETTER)
                          5 - 0 - 1 = 4(e)
                          5 - 1 - 1 = 3(l)
                          5 - 2 - 1 = 2(l)
                          5 - 3 - 1 = 1(o)
                          5 - 4 - 1 = 0(undefined)
    Code:
       //now charz will hold Hello, but there is no null terminating 
       //char after the o.  This means charz is a char array but not a 
       //string.  To make it a string you have to put a null char at the
       //end of charz manually.
    
       //at the end of the above loop i = length, which is where the
       //null char should go.  so:
       charz[i] = '\0';
    why does it have to be a string? either way it holds the same information. You can display the information the same way also. So the '\0' wouldserver no purpose, really

    Code:
       //now you can do this:
      cout<<charz;
    
      return 0;
    }

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    gotta disagree there, Denethor2000.

    char chars[6] = "elloH";

    means:

    chars[0] = 'o';
    chars[1] = 'l';
    chars[2] = 'l';
    chars[3] = 'e';
    chars[4] = 'H';
    chars[5] = '\0';

    and

    int length = strlen(chars) = 5;

    but:

    chars[length] = chars[5] = '\0';

    not

    chars[length] = chars[5] = 'H';

    char indexes (the values inside the square brackets) are numbered starting at zero, not 1. Therefore, 'H' may be the fifth letter in elloH, but it has index 4 in the array called chars.


    As to trying to output a char array that is not null terminated using the >> operator, good luck. If it could be done, I agree, there is no need for null termination of char arrays; but the very definition of string, and what differentiates a string from a plain char array is the fact that strings are null terminated. You can't do this to display all elements of the int array array below:

    int array[3] = {0};

    cout << array;

    and if you have this:

    char charz[6] = {'H', 'e', 'l', 'l', 'o'};

    you can't do this, either:

    cout << charz;


    the _only_ reason you can do this:

    cout << chars;

    is that chars _is_ a null terminated char array.

    To display all the elements is charz as defined above you need to use a loop; just like any other array that is not a string:

    for(int i = 0; i < 5; i++)
    {
    cout << charz[i];
    }


    That's the whole advantage of strings over non null terminated char arrays. Now you may say, but there is not null terminating char at the end of "elloH", and I'd say you're correct. But what neither of us said was that the compiler added the null terminating char for us in the declaration of chars. >> and getline() also add the null terminating char for us. However, if we are manipulating the char arrays ourselves, then we have to be sure the null terminating char is taken into account, or pay the price.

    Respectfullly,

    elad

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21