问题
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);
}
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.
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 .
回答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
Source:
#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;
}
Output:
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
You still have to implement the +1
part of two's complement.
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.
回答2:
Here's a much simpler implementation of what you're trying to do:
#include <stdio.h>
#include <stdint.h>
void binary_string(uint16_t number, char * output) {
for ( int i = 0; i < 16; ++i ) {
if ( number & 0x8000 ) {
output[i] = '1';
} else {
output[i] = '0';
}
number <<= 1;
}
}
void complement(uint16_t number, char * output) {
binary_string(number, output);
for (int x = 0; x < 16; x++) {
if (output[x] == '1') {
output[x] = '0';
} else {
output[x] = '1';
}
}
}
void complement_better(uint16_t number, char * output) {
binary_string(~number, output);
}
int main(void) {
char bits[17] = {0};
uint16_t number = 57801;
binary_string(number, bits);
printf("Before conversion: %s\n", bits);
complement(number, bits);
printf("After conversion : %s\n", bits);
return 0;
}
and outputs:
paul@MacBook:~/Documents/src/scratch$ ./tc
Before conversion: 1110000111001001
After conversion : 0001111000110110
paul@MacBook:~/Documents/src/scratch$
来源:https://stackoverflow.com/questions/21371246/replacing-a-character-in-a-c-string