im now currently doing function to deal wth char array...
i have a problem, is there a function or way that works like the Left() and Right() in visual basic ???
im using visual c++, win32 app.
Printable View
im now currently doing function to deal wth char array...
i have a problem, is there a function or way that works like the Left() and Right() in visual basic ???
im using visual c++, win32 app.
What does Left() and Right() do?
Magos: Left(String, 3) returns the 3 leftmost characters of a string, and right would return the rightmost ones.
monkeymon: No, you'd have to write you own, afaik.
There are no standard functions to do this but they are reasonably easy to write yourself with a little thought.....
<edit>Code:#include <stdio.h>
#include <string.h>
#include <conio.h>
char* Left(char* String, char* TempBuffer, int Nr)
{
if(strlen(String) >= Nr)
{
strcpy(TempBuffer, String);
TempBuffer[Nr] = '\0';
return TempBuffer;
}
else return String;
}
char* Right(char* String, char* TempBuffer, int Nr)
{
if(strlen(String) >= Nr)
{
strcpy(TempBuffer, &String[strlen(String) - Nr]);
TempBuffer[Nr] = '\0';
return TempBuffer;
}
else return String;
}
int main()
{
char MyString[] = "Hello world";
char TempBuffer[32];
printf("String: %s\n", MyString);
printf("Left(5): %s\n", Left(MyString, TempBuffer, 5));
printf("Right(5): %s\n", Right(MyString, TempBuffer, 5));
getch();
return 0;
}
Perhaps I shouldn't have use printf() on the C++ board :).
Well, I guess you can change it to cout if you like.
</edit>
what does this mean "&String"?Quote:
Originally posted by Magos
<edit>Code:#include <stdio.h>
#include <string.h>
#include <conio.h>
char* Left(char* String, char* TempBuffer, int Nr)
{
if(strlen(String) >= Nr)
{
strcpy(TempBuffer, String);
TempBuffer[Nr] = '\0';
return TempBuffer;
}
else return String;
}
char* Right(char* String, char* TempBuffer, int Nr)
{
if(strlen(String) >= Nr)
{
strcpy(TempBuffer, &String[strlen(String) - Nr]);
TempBuffer[Nr] = '\0';
return TempBuffer;
}
else return String;
}
int main()
{
char MyString[] = "Hello world";
char TempBuffer[32];
printf("String: %s\n", MyString);
printf("Left(5): %s\n", Left(MyString, TempBuffer, 5));
printf("Right(5): %s\n", Right(MyString, TempBuffer, 5));
getch();
return 0;
}
Perhaps I shouldn't have use printf() on the C++ board :).
Well, I guess you can change it to cout if you like.
</edit>
izzit capture the charaters into this variable?
&String means address of String.
Its just some basic pointer arithmetic to get the address of the char Nr chars from the right end of the string.