char

How to append '=' to a string

ぐ巨炮叔叔 提交于 2021-01-27 19:11:51
问题 I am trying to add = at the end of my array and then end it by appending a 0 This is how I allocate my space char* postExpr = malloc(sizeof(char)*MAX_LEN); I have tried many methods but I am still failing to append character '=' at the end of my string, every other character works just fine. What have I tried postExpr[postLen++] = 61; postExpr[postLen++] = '='; postExpr[postLen++] = infExpr[i]; in infExpr[i] is stored the value '=' EDIT: char* infix2postfix (const char* infExpr) { char*

remove spaces from char* array in C

浪子不回头ぞ 提交于 2021-01-27 17:51:20
问题 I am working on a plugin in C for a game emulator, where I would like to remove all spaces from chat message before checking if the input messages contain any website urls. So, I have this function like this to deblank (remove spaces from input message): char* deblank(char* input) { int i, j; char *output = input; for (i = 0, j = 0; i < strlen(input); i++, j++) { if (input[i] != ' ') output[j] = input[i]; else j--; } output[j] = 0; return output; } I use it in my plugin hook like this: bool

Bitwise operation on char gives 32 bit result

风格不统一 提交于 2021-01-27 17:45:39
问题 I've been writing a program in C to move the first 4 bits of a char to the end and the last 4 to the start. For most values it works normally, as well as the reverse operation, but for some values, as 8, x, y, z, it gives as result a 32 bit value. Values checked through printing hex value of the variable. Can anybody explain why this is happening? #include <stdio.h> #include <stdlib.h> int main() { char o, f,a=15; scanf("%c",&o); printf("o= %d\n",o); f=o&a; o=o>>4; printf("o= %d",o); o=o|(f<

Line break represented as “\r” on Mac?

大憨熊 提交于 2021-01-27 13:24:24
问题 See fiddle: if you press Enter in the textarea on a recent Mac, the newline character appears to be represented by \r , which would be in direct contrast to the answers to this, this and this question and to Wikipedia. To summarise these links: \r represents a line break on old Macs (OS 9 and before) \n represents a line break on UNIX systems (OS X, Linux) \r\n represents a line break on Windows. When I press Enter in the JSFiddle, I get the following result (OS X 10.11.6): textarea: \r span:

Why does C++ prints unsigned char value as negative?

懵懂的女人 提交于 2021-01-27 11:50:44
问题 I'm trying to understand the implicit conversion rules in C++ and I understood that when there are one operation between two primary types the "lower type" is promoted to the "higher type", so let say for: int a = 5; float b = 0.5; std::cout << a + b << "\n"; should print 5.5 because 'a' gets promoted to float type. I also understood that unsigned types are "higher types" than the signed counter parts so: int c = 5; unsigned int d = 10; std::cout << c - d << "\n"; prints 4294967291 because 'c

Why does C++ prints unsigned char value as negative?

坚强是说给别人听的谎言 提交于 2021-01-27 11:45:12
问题 I'm trying to understand the implicit conversion rules in C++ and I understood that when there are one operation between two primary types the "lower type" is promoted to the "higher type", so let say for: int a = 5; float b = 0.5; std::cout << a + b << "\n"; should print 5.5 because 'a' gets promoted to float type. I also understood that unsigned types are "higher types" than the signed counter parts so: int c = 5; unsigned int d = 10; std::cout << c - d << "\n"; prints 4294967291 because 'c

Concatenate char literal ('x') vs single char string literal (“x”)

不羁岁月 提交于 2021-01-20 15:31:40
问题 When I have a String that I need to concatenate a single char to its end, should I prefer s = .... + ']' over s = .... + "]" for any performance reason? I know array string joining and of String builders, and I am NOT asking for suggestions on how to concatenate strings in general. I also know some of would have the urge to explain to me about premature optimizations and that in general I should not bother with such minor stuff, please don't... I am asking because from a coding style

Check if all values entered into char array are numerical

情到浓时终转凉″ 提交于 2021-01-07 06:34:37
问题 I have a char array, where should be only numbers (10 digits). If the user enters letters or special characters (even among those digits) the program should prompt the user again to enter the number. I tried so many ways of doing it, but still couldn't find a way. That's what I could do so far: int f = 1; int i = 0; int flag =1; char num[11]; printf("Enter a number: "); while (f == 1) { scanf("%10s", num); while (flag == 1 && isdigit(num[i])) { i++; if (i == 10) { f = 0; flag =0; } } if (

字符串(C语言)

青春壹個敷衍的年華 提交于 2021-01-07 05:45:45
1.如果要声明一个字符串“NoMasp”,下面这行代码错在哪里? char name[] = { 'N' , 'o' , 'M' , 'a' , 's' , 'p' }; 如果想声明的是字符串,就需要在初始化时在结尾加上一个’\0’。或者可以直接用下面这张方式: char name[] = "NoMasp" ; 2.以下这段代码会打印出什么? #include <stdio.h> int main() { char nomasp[]= "I like C language." ; char *ptr; ptr=nomasp; ++ptr; nomasp[ 6 ]= '\0' ; puts (++ptr); return 0 ; } 在ptr执行自增操作之后就是指向字母’I’后的空格,而后在索引为6的地方改为’\0’因此字符串在这里就被截断了。所以最后打印出来的是”like”。 3.’A’一定比”A”更加节省空间吗? 不一定。字符常量是存储在int中的,也就是说’A’会占用2个或4个字节,虽然它’A’实际上只使用了一个字节来存储它的编码。而”A”则使用了2个字节,一个字节用来保存’A’,另一个字节用来保存’\0’。 char c = 'A' ; c作为字符变量则占用1个字节。 为使本文得到斧正和提问,转载请注明出处: http://blog.csdn.net/nomasp 版权声明

How do I add String to a char array?

别说谁变了你拦得住时间么 提交于 2021-01-07 00:56:17
问题 public static void main (String[] args) { char[][] c = {{'a', 'b', 'c'}, {'d', 'e', 'f'}}; show(c); } public static void show (char[][] c) { for (int i = 0; i < c.length; i++) { System.out.println(c[i]); I want a space between each letter. I tried to write + " " after c[i] but then I get this warning: "Must explicitly convert the char[] to a String". How I am supposed to add a string to my array? Thanks in advance! 回答1: Right now what you are doing wrong is, you are printing each sub-array. I