2011-02-02 86 views
0

我的字符串長度是恆定的,但實際的數據長度將像下面會發生變化,如何在字符串前添加減號? (在C)

"   1,54" // will be displayed as "- 1,54" 
"123456789012,12" // will be dsiplayed as "- 123456789012,12" 
+1

氣味功課... – 2011-02-02 17:02:51

+2

@Javed將是愚蠢的功課永遠 – 2011-02-02 17:06:09

+0

..功課,不過! – karlphillip 2011-02-02 17:07:42

回答

0

我下面的功能實現來執行我的要求。將輸入字符串轉換爲數字對我來說不起作用,因爲我的字符串有兩部分(「123,54」),逗號分隔。

static char * 
fmtsign(char * buffer) 
{ 
    char    tmpbuf[20]; 
    char    number[20]; 

    int i, len, space = 0; 

    memset(tmpbuf, 0x00, sizeof tmpbuf); 
    memset(number, 0x00, sizeof number); 

    len = (int)strlen(buffer); 

    for (i = 0; i < len; i++) 
    { 
      if (buffer[i] == ' ') 
      { 
       space++; 
      } 
      else 
       break; 
    } 

    strncpy(number, &buffer[space], len - space); 

    sprintf(tmpbuf, "- %s", number); 

    memset(buffer, 0x00, sizeof buffer); 

    strcpy(buffer, tmpbuf); 

    return (buffer); 
} 

輸出:

- 1234567,89 
    - 123,45 
     - 0,00 
0

我會做這樣的事情:

假設str1是老的字符串

int strLen1 = strlen(str1); 
char * newStr = malloc(sizeof(char) *(strLen1+ 2)); 
*newStr = '-'; 
++newStr; 
strcpy(newStr , str1); 

你可以避免strlen()全部一起做

char * newStr = malloc(sizeof(str1)+1); 
*newStr = '-'; 
++newStr; 
strcpy(newStr , str1); 

但請記住sizeof(str1)將返回什麼取決於您如何定義str1

不要忘記free()

5

那豈不是最簡單的只是把一個「 - 」格式的字符串時,顯示的數據?

printf("-%f", 1.54); 
4

我建議使用的sprintf()在這種情況下。

更新代碼,以便它丟棄在開始的時候所有的空格,但1.這樣,你將有-符號後面跟一個空格,然後是電話號碼。我在代碼中留下了一些註釋,以及一些註釋,如果需要,可以幫助您調試代碼。

#include <stdio.h> 

int main() 
{ 
    char* num = " 1,54"; 

    int c = 0; 
    if (strlen(num) > 0) 
    { 
     c = num[0]; // Safely storing the first character. Is it a whitespace? 
    } 
    else 
    { 
     printf("String is empty!\n"); 
     return -1; 
    } 

    int wspace_count = 0; // number of whitespaces detected 
    while (c == 32) // ASCII 32 = whitespace 
    { 
     c = num[wspace_count]; 
     if (c == 32) 
      wspace_count++; 
    } 

    //printf("whitespaces detected: %d\n", wspace_count); 
    //printf("total chars in num: %d\n", strlen(num)); 

    int chars_to_copy = strlen(num) - wspace_count+1; // +1 becouse you want to leave 1 whitespace there, right? 
    //printf("chars_to_copy: %d\n", chars_to_copy); 

    int tmp_size = chars_to_copy + 1; // another +1 becouse you need to append '\0' at the end 
    char* tmp = malloc(tmp_size); 
    int pos = wspace_count - 1; 
    strncpy(tmp, &num[pos], chars_to_copy); 
    tmp[strlen(tmp)] = '\0'; // add '\0' at the end 

    //printf("tmp = \"%s\" \n", tmp); 

    char* result = (char*) malloc(strlen(tmp) + 3); // 1 byte for the - sign, 1 for the space after it, 1 for the '\0' at the end 
    sprintf(result, "- %s", tmp); 

    printf("%s\n", result); 

    return 0; 
} 

輸出:

- 1,54 
0

我猜你不想被打印出來的空間,所以:

char str[LEN] = "   1,54"; 
char *nid = str; 
while (*nid == ' ') { 
    *nid = '-'; 
    nid++; 
} 
printf("%s", --nid); 
0

即時猜測THI是法國和, =十進制如果是這樣,然後按位操作和計算兩個補碼應該工作。

在僞

temp = 2700; 
newtemp = ~temp; 
newtemp = newtemp + 1;  //Two's compliment