character-arrays

Program skips cin.getline()

点点圈 提交于 2019-11-28 04:13:43
问题 I have made this program, It get the users adress, name and work. Then It puts it all into one string and outputs that string. (I know there are better ways to do this) char str[600]; char adrs[200]; char name[10]; char wrk[200]; cout<<"Enter your name and press ENTER: "; cin.getline(name,10); cout<<"\nEnter your adress and press ENTER:"; cin.getline(adrs,200); cout<<"\nEnter your workplace and press ENTER:"; cin.getline(wrk,200); strcpy(str,"My name is "); strcat(str,name); strcat(str,"\nand

Output of a C string program

时间秒杀一切 提交于 2019-11-27 08:18:35
问题 #include<stdio.h> int main() { char s[2]="a"; s[1]='b';s[2]='c';s[3]='d';s[5]='e'; printf("%s $%c$",s,s[4]); return 0; } 1.When I run this program in C (gcc-4.7.2) I expected Runtime Error because of the missing Null Character ('\0'). 2.If still the program compiles and executes successfully ,since s[4] has not been initialised,I expected some garbage value at that place..but here also I was wrong. The output of the above program is: abcde $$ There is no character between the two $(dollor)

Comparing character arrays and string literals in C++

六月ゝ 毕业季﹏ 提交于 2019-11-27 07:54:07
问题 I have a character array and I'm trying to figure out if it matches a string literal, for example: char value[] = "yes"; if(value == "yes") { // code block } else { // code block } This resulted in the following error: comparison with string literal results in unspecified behavior. I also tried something like: char value[] = "yes"; if(strcmp(value, "yes")) { // code block } else { // code block } This didn't yield any compiler errors but it is not behaving as expected. 回答1: std::strcmp

PHP - iterate on string characters

橙三吉。 提交于 2019-11-27 00:25:05
问题 Is there a nice way to iterate on the characters of a string? I'd like to be able to do foreach , array_map , array_walk , array_filter etc. on the characters of a string. Type casting/juggling didnt get me anywhere (put the whole string as one element of array), and the best solution I've found is simply using a for loop to construct the array. It feels like there should be something better. I mean, if you can index on it shouldn't you be able to iterate as well? This is the best I've got

How do you get a string to a character array in JavaScript?

拈花ヽ惹草 提交于 2019-11-26 03:01:43
How do you get a string to a character array in JavaScript? I'm thinking getting a string like "Hello world!" to the array ['H','e','l','l','o',' ','w','o','r','l','d','!'] meder omuraliev Note: This is not unicode compliant. "I💖U".split('') results in the 4 character array ["I", "�", "�", "u"] which can lead to dangerous bugs. See answers below for safe alternatives. Just split it by an empty string. var output = "Hello world!".split(''); console.log(output); See the String.prototype.split() MDN docs . hakatashi Since this question is originally asked more than five years ago, people are

How do you get a string to a character array in JavaScript?

本秂侑毒 提交于 2019-11-26 00:57:28
问题 How do you get a string to a character array in JavaScript? I\'m thinking getting a string like \"Hello world!\" to the array [\'H\',\'e\',\'l\',\'l\',\'o\',\' \',\'w\',\'o\',\'r\',\'l\',\'d\',\'!\'] 回答1: Note: This is not unicode compliant. "I💖U".split('') results in the 4 character array ["I", "�", "�", "u"] which can lead to dangerous bugs. See answers below for safe alternatives. Just split it by an empty string. var output = "Hello world!".split(''); console.log(output); See the String