Thread: Removing char from string

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    8

    Removing char from string

    /scan word from file/

    do
    {
    wordscan = fscanf(f, "%s", buffer);
    if (wordscan != EOF)
    {
    if(strstr(buffer, filter))
    {
    temp = buffer;
    charstart = strlen(strstr(temp, "file:/") - 1);
    charend = strlen(temp);
    printf("%s, %d\n", temp, charend);
    }
    }
    } while (wordscan != EOF);
    printf("File: %s is processed. \n", argv[i]);

    /end code/

    Hi, need some help regarding some issues in the above codes. I have managed to extract word from the file but I need only specific text from a long string eg href:/abc and I need only abc. I know the position of a as well as c.....I have tried using for loops to retrieve abc but it did not seems to work. What could possibly be wrong??

    And is the code above looks alrite??

    Thanks

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Study this one, it may help you

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	char	data[] = "href:/abc";
    	char	*ptr;
    
    	printf("The full string is %s\n", data);
    
    	if ((ptr = strchr(data, '/')) != NULL)
    	{
    		ptr++;
    		printf("The second part is %s\n", ptr);
    	}
    
    	return(0);
    }
    Please use code tags in future, too.
    Last edited by Hammer; 05-22-2002 at 08:17 AM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    8
    Thanks Hammer...

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hammer's solution is really just a work around. It doesn't actually remove anything from the string.

    To actually remove an item, you need to do the following:
    Code:
    while not what I'm looking for
    {
        skip ahead to the next character
    }
    
    do
    {
        copy the next character to the current location
        move to the next location
    }
    while not the end of the string
    Given the above pseudo code, you shouldn't have any problems getting the task done.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by quzah
    Hammer's solution is really just a work around. It doesn't actually remove anything from the string.
    That depends on how you read the requirement
    Posted by cruxxe
    I need only specific text from a long string eg href:/abc and I need only abc.
    My solution got a pointer to.... well, you can see what it does.

    Admittedly the title of this post is "Removing char from string", but as cruxxe gave a specific example, that's the one I chose to address.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Hammer

    That depends on how you read the requirement

    My solution got a pointer to.... well, you can see what it does.

    Admittedly the title of this post is "Removing char from string", but as cruxxe gave a specific example, that's the one I chose to address.
    Very true. I just went by the title of the thread. This is one of those 'I want this, no I mean this.'

    Quzah.
    Hope is the first step on the road to disappointment.

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. Removing the last char from a string
    By John_L in forum C# Programming
    Replies: 2
    Last Post: 02-29-2008, 09:42 AM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM