• C

String delimition and manipulation in C

Hi,
I have a string manipulation questions in C, and for the question assume I have the same input which is at least ten characters in length (maybe even more). How do I 'trim' an array of characters, for example if i had a char array[10] = "0123-45 67-89" to become "0123456789" (replacing all the dash and spaces with ''). Is there a built in function which does this other than a tokenizer? Also is there a replace a character in a string function in C?( for example in the string A234 search for all the A/a's and replace it with 1)
Another side note is how do i turn characters into integers?
Thanks!
LVL 3
MytixAsked:
Who is Participating?
 
ankuratvbConnect With a Mentor Commented:
AFAIK there is no standard function defined but you can always write your own function.

For Instance:

For removing NonDigits(InPlace,no other array used,if you use one,then the speed complexity will increase as you'll only traverse the string once and there wont be any shifting of characters in the string.)

void RemoveNonDigit(char arr[])
{
 int i=0;
 while(arr[i]!='\0')
 {
  if(isdigit(arr[i])==0)
  {
   int c=i;
   while(arr[c]!='\0')
      {arr[c]=arr[c+1];c++;}
  }
  i++;
 }
}

For Replacing one character with another character:

void replace(char str[],char prev,char next)
{
 int i=0;
 while(str[i]!='\0')
 {
  if(str[i]==prev) str[i]=next;
  i++;
 }
}


Please note that the string you pass to these functions must be modifiable.
If you pass replace("Hello",'H','i');
the program may seg fault because string literals may be stored in read only memory.
0
 
avizitConnect With a Mentor Commented:
I have only part of the answer
to convert characters into integers use  atoio()


NAME
       atoi, atol, atoll, atoq - convert a string to an integer.

SYNOPSIS
       #include <stdlib.h>

       int atoi(const char *nptr);
       long atol(const char *nptr);
       long long atoll(const char *nptr);
       long long atoq(const char *nptr);

DESCRIPTION
       The atoi() function converts the initial portion of the string pointed to by nptr to int.  The behaviour is the same as

              strtol(nptr, (char **)NULL, 10);

       except that atoi() does not detect errors.

       The atol() and atoll() functions behave the same as atoi(), except that they convert the initial portion of the string to
       their return type of long or long long.  atoq() is an obsolete name for atoll().

RETURN VALUE
       The converted value.
0
 
PaulCaswellConnect With a Mentor Commented:
>>How do I 'trim' an array of characters, for example if i had a char array[10] = "0123-45 67-89" to become "0123456789" (replacing all the dash and spaces with '').
You do it yourself.

>>Is there a built in function which does this other than a tokenizer?
No.

>>Also is there a replace a character in a string function in C?( for example in the string A234 search for all the A/a's and replace it with 1)
No.

>>Another side note is how do i turn characters into integers?
You use avizit's idea of 'atoi' or 'atol'.

Now we've got the negative stuff out of the way, lets get something positive to say.

>>You do it yourself.
But its really easy. Use something like.

char * Remove(char * str, char * remove);
{
 char * src = str;
 char * dest = str;

 while ( *src != '\0' )
 {
  // Only copy the character if its not in the remove string.
  if ( strchr ( remove, *src ) == NULL ) *dest++ = *src;
  src++;
 };
 *dest = '\0';
 return (str);
}

And use it like this:

...
 char string [30];
 strcpy (string,"0123-45 987");
 Remove ( string, "-_ \t\r\n" );
...

Paul
0
Managing Security Policy in a Changing Environment

The enterprise network environment is evolving rapidly as companies extend their physical data centers to embrace cloud computing and software-defined networking. This new reality means that the challenge of managing the security policy is much more dynamic and complex.

 
alikoankCommented:
take a look at www.snippets.com they have lots of small code fragments (aka snippets) on topics like this.
0
 
ankuratvbConnect With a Mentor Commented:
Hi alikoank,

AFAI remember it was snippets.org rather than snippets.com

Goto:
http://c.snippets.org

for the C codes.
0
 
alikoankCommented:
oops. my mistake :-)

thanks for correction.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.