is there any function which convert the char array into its invert starting from the end and then copy it to another char array?
is there any function which convert the char array into its invert starting from the end and then copy it to another char array?
strrev(); might work.
why not make your own?
output: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;
this is NOT tested !Hello
Last edited by Denethor2000; 04-30-2002 at 11:22 AM.
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
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
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.
lol oops. ill put it on 1024
--------------------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)
sorry im not at home i dont have a compiler at school so ur gonna have to edit it a bit k?
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; }
this might not be std but i'll do it like this
Code:char str1[12],str2[12]; -- -- strcpy(str1,str2); strrev(str2); -- --
-
Then again...
CString sTemp;
sTemp = "Hello";
sTemp.MakeReverse();
That's kinda cheating... oh well!
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
LENGTH - I - 1 would make this: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)
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)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, reallyCode://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';
Code://now you can do this: cout<<charz; return 0; }
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