954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Removing found chars from string

Hello all

I want to be able to search a string and remove non a-z chars and also search for certain words and then remove them

ie. char string[25]="STEPHEN JOHNSON - LTD"

So i use isalpha to get rid of the non a-z string

i am left with STEPHENJOHNSONLTD

LTD is the word i want to remove

How do i go about this?

Any help is much appreciated.

sgriffiths
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 9
Solved Threads: 0
 

If other strings are similar remove everything after the second space.

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 

to get rid of non-alpha characters -- use a pointer to check each char using isalpha() macro. When non-alpha char found, call movemem() to shift all remaining characters lincluding the null terminator left one byte to cover up the non-alpha character.

After you get that working we cal talk about removing the word. Don't try to do all of it all at the same time or you will have trouble doing any of it.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I have managed to get my string from

"STEPHEN JOHNSON - LTD"

to

"STEPHENJOHNSONLTD"

No i need to remove LTD

Thanks

sgriffiths
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 9
Solved Threads: 0
 

First form your string like "STEPHEN JOHNSON".

for (i=0; i<25; i++, string++)
{
   if (*string == '-' && i != 0)
   {
      *(string-1) = '\0';
      
   }
}

and then remove the space.

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 

But this doesnt help me remove a word as such does it?

I need to remove the word LTD from string

STEPHENJOHNSONLTD

Thanks

sgriffiths
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 9
Solved Threads: 0
 

First form your string like "STEPHEN JOHNSON".

for (i=0; i<25; i++, string++)
{
   if (*string == '-' && i != 0)
   {
      *(string-1) = '\0';
      
   }
}

and then remove the space.

why does the loop count to 25? What if the string does not contain 25 characters, or if it contains more than 25 characters. Hard-coding a number there is not a good idea.

>> if (*string == '-' && i != 0)
syntax error.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
But this doesnt help me remove a word as such does it? I need to remove the word LTD from string STEPHENJOHNSONLTD Thanks

use strstr() to find beginning of the word you want to remove, then use a pinter to shift everything left until either a space is found of end of string is found. Now lets see you post some code.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

why does the loop count to 25? What if the string does not contain 25 characters, or if it contains more than 25 characters. Hard-coding a number there is not a good idea.

>> if (*string == '-' && i != 0) syntax error.


Hm read the first postie. char string[25]="STEPHEN JOHNSON - LTD"
Whats the posibility that the string have more than 25 chars?

#include <stdio.h>

int main()
{
   char p[25] = "STEPHEN JOHNSON - LTD";
   char *string = p;
   unsigned char i;
   printf("%s\n", p);
   
   for (i=0; i<25; i++, string++)
   {
      if (*string == '-' && i != 0)
      {
         *(string-1) = '\0';
         break; /* added this line */
      }
   }
   printf("%s\n", p);
   return 0;
}

Well I didn't posted the whole code.
Only thing is that I should break when '-' found.
EDIT: Simpler than using strlen

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 
Hm read the first post


Yes, I misread it. That is a good example of why the programmer should use pleanty of white space..whats the posibility that the string have more than 25 chars?

100 % when the string contains some other characters. Loops like that should be made more general to handle strings of any length. Hard-coding the string length is just sloppy programming.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
#include <stdio.h>
#include <string.h>

void main()
{

   char string[] = "STEPHEN JOHNSON - LTD";
   char key[]    = "LTD";

   int i = 0;
   size_t length = 0;
   size_t keylen = 0;


   length = strlen( string );
   keylen = strlen( key    );

   /*
   ** Up to this point, the code is self-explanatory.
   ** Now, cycle through the string and search for the key.
   ** Notice "length - keylen" prevents overrun of memory compare
   ** Once the key is found, substitute it with spaces.
   ** Thereafter, you can do the cleanup of removing non a-z chars as norm.
   **
   ** Hint: key is set up as an array so that you 'could' do all of this
   ** in a function; just pass the string, the key, and the replacement char
   ** and the function "my_replace" will convert it for you.
   */
   for ( i = 0; i <= length - keylen; i++ )
   {
      if ( 0 == memcmp( &string[i], key, keylen ))
      {
         memset( &string[i], ' ', keylen );
      }
   }

   //...
   //...
   //...
}
mkadwa
Newbie Poster
2 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You