How to delete string after >

This is a discussion on How to delete string after > within the C Programming forums, part of the General Programming Boards category; char *string = "Hello how are you doing > file"; char *string2 = "hello > file how are you doing"; ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    41

    How to delete string after >

    char *string = "Hello how are you doing > file";
    char *string2 = "hello > file how are you doing";

    I wanted to to store the information in string one as

    char *stringx = "Hello how are you doing";
    char *string2x = "hello how are you doing";

    How can i archieve this ?? I just wanted to remove the ">" and the string after ">" only.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    just run through the string till you hit the > and then replace it with a null terminator. '\0'

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well the problem is that since you are defining the character string as:
    Code:
    char *string2 = "hello > file how are you doing";
    you can not just add in a null since most implamentations put it in read only memory.
    if you change it to
    Code:
    char string2[] = "hello > file how are you doing";
    then you can

  4. #4
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by winsonlee
    char *string = "Hello how are you doing > file";
    char *string2 = "hello > file how are you doing";

    I wanted to to store the information in string one as

    char *stringx = "Hello how are you doing";
    char *string2x = "hello how are you doing";

    How can i archieve this ?? I just wanted to remove the ">" and the string after ">" only.
    First of all, you should learn the difference between a pointer to a string literal (what you have written)
    Code:
       char *p = "string";
    and an array of char initialized by a string, like
    Code:
       char p[] = "string";
    In the first case, you have a pointer to char that holds the address of an array of char terminated by a 0. This address belongs to a segment of memory that is not guaranteed to be mutable. (Actually, on embedded systems, it belongs to a segment that is implemented in *ROM or flash memory)

    This is why it is highly recommended to define such pointers like this:
    Code:
       char const *p = "string";
    It helps to prevent from a common error that is to attempt to write to a non-writable segment of memory.

    On the second case, you have an array of char. Can be a string or not. The value of the elements can be changed. Just keep in mind that its size is defined by its initialization. It can't be rezised dynamically.
    Code:
    {
       /* an array of 10 uninitialized char */
       char a[10];
    
       /* an array of char initialized with {'h','e','l','l','o',0} (valid string) */
       char a[] = "hello";
    
       /* an array of 10 char initialized with {'h','e','l','l','o',0, 0, 0, 0, 0} (valid string) */
       char a[10] = "hello";
    
       /* an array of 5 char initialized with {'h','e','l','l','o'} (Not a valid string) */
       char a[5] = "hello";
    }
    All this introduction to say that
    Code:
    char *string = "Hello how are you doing > file";
    char *string2 = "hello > file how are you doing";
    should have been defined
    Code:
    char const *string = "Hello how are you doing > file";
    char const *string2 = "hello > file how are you doing";
    and is not mutable.

    If you want to change these strings, according to your wishes, you can start with that:
    Code:
       char string[] = "Hello how are you doing > file";
       char string2[] = "hello > file how are you doing";
    Then you have to define clearly what you want. I guess you want to suppress the " > file" substring.
    • Search the position of the substring (strstr())
    • Move the end of the string at this place (memmove())

    It's now your turn to play.
    Last edited by Emmanuel Delaha; 08-08-2004 at 01:59 AM. Reason: typo
    Emmanuel Delahaye

    "C is a sharp tool"

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    41
    The problem is both the string is from the standard input. So i cant declare string as string[]. My second problem is the name after ">" always change. Sometimes it can be file1 sometimes it can be xxfile11. so how can i use strstr and memmove to solved this problem ??

  6. #6
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by winsonlee
    The problem is both the string is from the standard input. So i cant declare string as string[].
    Use
    Code:
       char sa[123];
       char sb[123];
    for example.
    My second problem is the name after ">" always change. Sometimes it can be file1 sometimes it can be xxfile11.
    If it changes, you must define a format that delimitates the data. It's a specification issue.
    so how can i use strstr and memmove to solved this problem ??
    This is the point. Open your book, and shake your brain. I'm doing the job for you. It's your assignment.
    Emmanuel Delahaye

    "C is a sharp tool"

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. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM

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