Solved

String delimition and manipulation in C

Posted on 2004-09-30
6
365 Views
Last Modified: 2011-08-18
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!
0
Comment
Question by:Mytix
6 Comments
 
LVL 11

Assisted Solution

by:avizit
avizit earned 20 total points
Comment Utility
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
 
LVL 16

Assisted Solution

by:PaulCaswell
PaulCaswell earned 140 total points
Comment Utility
>>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
 
LVL 9

Accepted Solution

by:
ankuratvb earned 40 total points
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 4

Expert Comment

by:alikoank
Comment Utility
take a look at www.snippets.com they have lots of small code fragments (aka snippets) on topics like this.
0
 
LVL 9

Assisted Solution

by:ankuratvb
ankuratvb earned 40 total points
Comment Utility
Hi alikoank,

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

Goto:
http://c.snippets.org

for the C codes.
0
 
LVL 4

Expert Comment

by:alikoank
Comment Utility
oops. my mistake :-)

thanks for correction.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now