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
Solved

String delimition and manipulation in C

Posted on 2004-09-30
6
369 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

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

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

860 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