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.
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?
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
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.
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
There are no standard functions to do this but they are reasonably easy to write yourself with a little thought.....
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
<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>
Last edited by Magos; 09-28-2002 at 01:18 PM.
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
what does this mean "&String"?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.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi