在C字符串替换字符 [英] Replacing a character in a c string

查看:174
本文介绍了在C字符串替换字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的C字符串替换的麻烦人物。我必须初始化为0和1的16位串C字符串名为位。我正在试图做的是转换成字符串的二进制补码的版本。我所学到的是一个简单的任务,如

I'm having trouble replacing characters in my c string. I have a c string called bits initialized to a sixteen bit string of 0's and 1's. What I'm trying to do is convert the strings into their twos complement versions. What I've learned is a simple assignment such as

int twosComplement(int number,char *binary){
    printf("searching for twos complement\n");
    int temp=number * -1;
    if(temp<-32768)
        return 0;
    printf("%d\n",temp);
    char bits[17]="";
    int i;
    int x=0;
    int y;
for(i=15;i>=0;i--){
    y=pow(2,i);
    if(temp%y!=temp){
        temp=temp%y;
        strcat(bits,"1");;
    }
    else{
        strcat(bits,"0");
    }
    printf("%s\n",bits);
    x++;
}

for(x=0;x<16;x++){
    if(bits[x]=='0'){
        *bits="a";
    }
    else{
        strcat(bits,"1");
    }
    printf("%s\n",bits);
}

在C illegial因为位实际上是一个指向字符串的第一个字符所以抱怨的整数分配不强制转换为指针。

is illegial in C because bits is actually a pointer to the first character in the string so it complains about an assignment from integer to pointer without a cast.

以上是code此功能。第一部分工作正常,并创建正数的正常16位重新presentation。在接下来的部分,我想看看字符串的每一个字符,并与备选字符替换。这code编译但不会因为IM串联工作。另外,我不认为这是正确读取每个字符是数字。

The above is code for this function. The first part works correctly and creates the proper 16 bit representation of the positive number. In the next part, i want to look at each character of the string and replace with the alternate character. This code compiles but doesn't work because im concatenating. Also, I don't think it's reading properly what digit each character is .

推荐答案

这code完全编译使用GCC 4.8.2在Mac OS X 10.9.1小牛用命令行:

This code compiles cleanly using GCC 4.8.2 on Mac OS X 10.9.1 Mavericks with the command line:

gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
        -Wold-style-definition -Werror bits.c -o bits

来源:

#include <math.h>
#include <stdio.h>
#include <string.h>

extern int twosComplement(int number);

int twosComplement(int number)
{
    printf("searching for twos complement of %d\n", number);
    int temp = number * -1;
    if (temp < -32768)
        return 0;
    printf("%d\n", temp);
    char bits[17] = "";
    int i;
    int x = 0;
    int y;
    for (i = 15; i >= 0; i--)
    {
        y = pow(2, i);
        if (temp % y != temp)
        {
            temp = temp % y;
            strcat(bits, "1");
        }
        else
        {
            strcat(bits, "0");
        }
        printf("%s\n", bits);
        x++;
    }

    printf("One's complement:\n");
    for (x = 0; x < 16; x++)
    {
        if (bits[x] == '0')
            bits[x] = '1';
        else
            bits[x] = '0';
        printf("%s\n", bits);
    }
    return 0;
}

int main(void)
{
    twosComplement(23);
    return 0;
}

输出:

searching for twos complement of 23
-23
0
00
000
0000
00000
000000
0000000
00000000
000000000
0000000000
00000000000
000000000001
0000000000010
00000000000101
000000000001011
0000000000010111
One's complement:
1000000000010111
1100000000010111
1110000000010111
1111000000010111
1111100000010111
1111110000010111
1111111000010111
1111111100010111
1111111110010111
1111111111010111
1111111111110111
1111111111100111
1111111111101111
1111111111101011
1111111111101001
1111111111101000

您还是要落实 +1 补的一部分。

You still have to implement the +1 part of two's complement.

警惕在这样的循环中使用的strcat()的。它导致二次运行时的行为的strcat()有添加新角色之前跳过的previous内容,因此它跳过0 + 1 + 2 + 3 + 4 + ...,N-1个字符,它是N(N-1)/ 2个字节跳过总

Be wary of using strcat() in a loop like this. It leads to quadratic runtime behaviour as strcat() has to skip over the previous content before adding the new character, so it skips 0+1+2+3+4+...N-1 characters, which is N(N-1)/2 bytes skipped in total.

这篇关于在C字符串替换字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆