hello all,
i'm trying to copy a string leaving out some of the characters.
example:
$4,000.00 > 4000.00
i need to get rid of the '$' and ','.
thank you in advance.
the cman
hello all,
i'm trying to copy a string leaving out some of the characters.
example:
$4,000.00 > 4000.00
i need to get rid of the '$' and ','.
thank you in advance.
the cman
look up ispunct() in your helpfiles.
im off to bed. cant give example too tired.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
Thank You. ispunct worked fine BUT
I need to preserve the '.' punct.
Example of my code output
input = dollar$4,000.00
current output = 400000
DESIRED OUTPUT = 4000.00
anyone got any ideas????
thanks
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char str1[100];
char str2[200];
char str3[200];
int main( void )
{
int i;
int a = 0;
scanf("%s", str1);
strcpy(str2, str1);
for( i = 0; i < strlen(str2); i++ ) {
if( isalpha(str2[i])) {
continue;
}
if( ispunct( str2[i]) ) {
// printf( "Char %c is a punctuation character\n",
//str2[i] );
continue;
} else {
// printf( "Char %c is not a punctuation character\n",
// str2[i] );
str3[a] = str2[i];
a++;
}
}
printf("str1= %s\n", str1);
printf("str2=%s\n", str2);
printf("str3=%s\n", str3);
return EXIT_SUCCESS;
}
That should suffice. You could nest your if statements, but I'll leave the fine tuning to you.Code:ptr = strchr( inputstring, '$' ); if( ptr != NULL ) ptr2 = strchr( ptr, ' ' ); if( ptr2 != NULL ) strncpy( buffer, ptr, ptr2-ptr );
Quzah.
Hope is the first step on the road to disappointment.
You could write your own strcpy function that passes the chars to not to be copied.
Code:void mystrcpy(char *s1, char *s2, char *cskip) { while(*s2) { char *c = cskip; int iskip=0; *s1 = *(s2++); while(*c) if(*s1 == *(c++)) iskip=1; if(!iskip) s1++; } *s1 = 0; } void main() { char s1[20], *s2 = "$4,000.00"; mystrcpy(s1, s2, "$,"); printf("%s %s\n",s1, s2); }
>void main()
This is wrong, main only returns an int. To be more specific, the correct definition of main when accepting no arguments is:
The other portable definition of main being one that takes two arguments:Code:int main ( void )
Where the types and identifiers of the parameters can be anything equivalent to the above.Code:int main ( int argc, char *argv[] )
-Prelude
Last edited by Prelude; 03-07-2003 at 10:53 AM.
My best code is written with the delete key.
>>void main()
yes, you're quite right, here's the reason why.
http://users.aber.ac.uk/auj/voidmain.shtml
Cheers....
What about something like?
Hammer... ah, a chance for me to try pointers... is it close?Code:#include <stdio.h> #include <stdlib.h> #include <ctype.h> int main(void) { char string[] = "$4,000.00", *ptr; int i, j; j=0; /* other code for inputting string */ if((ptr = (char *) malloc(sizeof(char) * strlen(string))) == NULL) { perror("Couldn't allocate memory"); return -1; } for(i=0; string[i] != '\0'; i++) if(isdigit(string[i]) || string[i] == '.') ptr[j++] = string[i]; ptr[j] = '\0'; printf("%s\n", ptr); return 0; }
Well I suppose that the string could have multiple occurrences of '.' so maybe it's not the best idea afterall.
>is it close?
Yes, but there are a few gotchas:
>if((ptr = (char *) malloc(sizeof(char) * strlen(string))) == NULL)
There's no need for sizeof(char) since it always evaluates to 1, and strlen returns a size_t value. Using sizeof(char) serves no purpose in this case. You shouldn't cast malloc since it may hide the error of not including stdlib.h, and you forgot to include string.h yet used strlen, this results in undefined behavior. And lastly, you forgot to att 1 to the size you ask for to account for the nul character. strlen does not include it.
>return -1;
-1 is an implementation defined return value, it is not portable. A better way would be to use EXIT_FAILURE which is defined in stdlib.h.
>return 0;
Since you used EXIT_FAILURE before, using EXIT_SUCCESS here would be good for consistency. You also neglected to free the memory you allocated previously, this is a bad thing.
-Prelude
My best code is written with the delete key.
Thanks for that Prelude. Some of those flubs just follow my signature.Originally posted by Prelude
>is it close?
Yes, but there are a few gotchas:
free(ptr); there
What I'd really like to see is a textbook written by the people that have helped me the most, if not directly, then by reading their replies to others -- Salem, Hammer, Prelude, and Quzah. If Cela is still around, then add that person too.
Without these folks, I'd still be using void main.