Reformating a number

Posted on 2006-05-26
Last Modified: 2010-04-15
I need to add something to some C code, but I am not efficient in C.

I have a number


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.


Question by:jmsloan
    LVL 45

    Accepted Solution

    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);
           printf("no -sign found");
    LVL 14

    Assisted Solution

    >      memcpy(number+1, number, len);

    better change that into memmove (
            memmove(number+1, number, len);
    LVL 45

    Expert Comment

    thats right ... change it to memmove .. thanks cwwkie

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    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…
    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    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 opening and reading files in the C programming language.

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now