Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

String delimition and manipulation in C

Posted on 2004-09-30
6
Medium Priority
?
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 11

Assisted Solution

by:avizit
avizit earned 60 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 420 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 120 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 120 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

618 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