Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-19-2005, 05:00 AM   #1 (permalink)
WGP
Registered User
 
Join Date: Aug 2005
Posts: 2
WGP is on a distinguished road
Remove/Insert Characters

Two questions...

1. How do I find and remove a character in a string?
For example with "1,200,000" I need to strip off the commas.

2. How would I insert a character?
For example with "18000" I need to insert a dot before last 2 digits (180.00).
Last 2 digits will not always be zeros.

WGP is offline   Reply With Quote
Old 08-19-2005, 06:02 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,709
redhead is on a distinguished road
I'll let the code speak for itself
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  int i, j, n;
  char str1[] = "1.200.00,313";
  char *str2;
#ifdef ONE
  /* make enough room to copy the number */
  str2 = (char*)malloc(strlen(str1)*sizeof(char));
  for(i=0,j=0;str1[i];++i)
    {
      if(str1[i] == '.' || str1[i] == ',')
	continue;/* skip it */
      str2[j++] = str1[i];
    }
#elif TWO
  /* make enough room for the comma */
  str2 = (char*)malloc((strlen(str1)+1)*sizeof(char));
  /* backwards copying */
  for(i=strlen(str1),j=strlen(str1)+1,n=0; i>=0; --i,--j, n++)
    {
      str2[j] = str1[i];
      if(n==2)
	str2[--j] = '.';
    }
#else
  printf("Please compile with ONE or TWO defined\n");
  printf("ie: gcc -DONE\n");
  return 1;
#endif
  str2[j] = '\0';
  printf("str1: %s\n", str1);
  printf("str2: %s\n", str2);
  free(str2);
  return 0;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is online now   Reply With Quote
Old 08-19-2005, 02:23 PM   #3 (permalink)
WGP
Registered User
 
Join Date: Aug 2005
Posts: 2
WGP is on a distinguished road
Thanks redhead!
Awesome....
WGP is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
regex for allowed characters in a form field DogTags PHP 1 03-13-2005 08:42 AM
ncurses won't display anything but keyboard characters! Hosiah Linux / BSD / OS X 3 02-01-2005 06:36 PM
removing ^M characters? Admin PHP 10 11-19-2004 04:07 AM
Multi Byte Characters with PHP/MySQL sde PHP 1 03-21-2004 01:56 PM
How can I count characters in a text field? jimmyoctane PHP 2 10-18-2003 03:38 PM


All times are GMT -8. The time now is 11:24 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting