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.

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.

I have managed to get my string from

"STEPHEN JOHNSON - LTD"

to

"STEPHENJOHNSONLTD"

No i need to remove LTD

Thanks

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.

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

I need to remove the word LTD from string

STEPHENJOHNSONLTD

Thanks

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.

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.

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 post

ie. 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

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.

#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 );
      }
   }

   //...
   //...
   //...
}
This article has been dead for over six months. Start a new discussion instead.