Solved

String delimition and manipulation in C

Posted on 2004-09-30
6
366 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
ID: 12197620
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
ID: 12198679
>>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
ID: 12198700
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 4

Expert Comment

by:alikoank
ID: 12200864
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
ID: 12200968
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
ID: 12201003
oops. my mistake :-)

thanks for correction.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Describe order of operation while copying text 11 115
Finding a good hash function 4 120
Line meaning 9 82
delete-remove 14 65
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

895 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

13 Experts available now in Live!

Get 1:1 Help Now