Reformating a number

I need to add something to some C code, but I am not efficient in C.

I have a number

12.25-

I would like to remove the minus sign and add it to the front, but all numbers are not negative.  So I first need to check and see if a minus sign exists, if it does take it out and put at the front.

Thanks,

jmsloan
LVL 3
jmsloanAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
sunnycoderConnect With a Mentor Commented:
Hi jmsloan,

you did not specify the format in which your number exists ... However from the question it appears that what you have is a string ... Assuming the same, this would solve your problem ...

char  number[] = "12.25-";
char * ptr = strchr (number,'-'); //strrchr would be faster here
if (ptr)
{
        int len = strlen(number);
        memcpy(number+1, number, len);
        number[len]='\0';
        number[0]='-';
}
else
       printf("no -sign found");
0
 
cwwkieConnect With a Mentor Commented:
>      memcpy(number+1, number, len);

better change that into memmove (http://cplusplus.com/ref/cstring/memmove.html):
        memmove(number+1, number, len);
0
 
sunnycoderCommented:
thats right ... change it to memmove .. thanks cwwkie
0
All Courses

From novice to tech pro — start learning today.