String Problem

This is a discussion on String Problem within the C++ Programming forums, part of the General Programming Boards category; im now currently doing function to deal wth char array... i have a problem, is there a function or way ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    77

    String Problem

    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.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    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

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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;
    }
    <edit>
    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 02: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.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    77
    Originally posted by Magos
    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;
    }
    <edit>
    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"?
    izzit capture the charaters into this variable?

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    &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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM

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