character-arrays

C++ convert strings to char arrays

…衆ロ難τιáo~ 提交于 2019-12-06 02:05:04
I want to have 2 strings as input so I can use getline(cin,s) (so I can pick the whole line until '\n' ) and then I want to search the second array if it contains the word of the first array without using string::find() or strstr() . But I still can't find a way to either convert strings to arrays int main() { string s; string s2; char array[50]; char array2[50]; cout<<"Give me the first word"<<endl; getline(cin,s); cout<<"Give me the text"<<endl; getline(cin.s2); array=s; array2=s2; } The second way I was thinking was doing the job from the start with arrays: char array[50]; cin.getline(array

using C Pointer with char array

*爱你&永不变心* 提交于 2019-12-04 11:58:58
int i=512; char *c = (char *)&i; c[0] =1; printf("%d",i); this displays "513", it adds 1 to i. int i=512; char *c = (char *)&i; c[1] =1; printf("%d",i); whereas this displays 256. Divides it by 2. Can someone please explain why? thanks a lot Binary The 32-bit number 512 expressed in binary, is just: 00000000000000000000001000000000 because 2 to the power of 9 is 512. Conventionally, you read the bits from right-to-left. Here are some other decimal numbers in binary: 0001 = 1 0010 = 2 0011 = 3 0100 = 4 The Cast: Reinterpreting the Int as an Array of Bytes When you do this: int i = 512; char *c

Finding derivative of a function stored in a character array

江枫思渺然 提交于 2019-12-02 14:43:31
问题 What I need to do is read a file which contains equations. I need to take the derivative of each equation and then write those derivative equations in a different .txt file. I've read all the equations into an array of character arrays and now I don't know what to do once I've stored them into the array. I really don't need help writing the equations into another file; I know I can figure that out. What I need help on is finding a way to taking the derivative of the functions. The type of

Finding derivative of a function stored in a character array

耗尽温柔 提交于 2019-12-02 09:37:29
What I need to do is read a file which contains equations. I need to take the derivative of each equation and then write those derivative equations in a different .txt file. I've read all the equations into an array of character arrays and now I don't know what to do once I've stored them into the array. I really don't need help writing the equations into another file; I know I can figure that out. What I need help on is finding a way to taking the derivative of the functions. The type of equations that are going to be read are not that complicated; they're going to be polynomials that don't

What does the output of a printed character array mean?

风流意气都作罢 提交于 2019-12-02 06:48:41
问题 I'm moving from C to Java now and I was following some tutorials regarding Strings. At one point in the tutorials they showed instantiating a new string from a character array then printing the string. I was following along, but I wanted to print both the character array and the string so I tried this: class Whatever { public static void main(String args[]) { char[] hello = { 'h', 'e', 'l', 'l', 'o', '.'}; String hello_str = new String(hello); System.out.println(hello + " " + hello_str); } }

atoi on a character array with lots of integers

China☆狼群 提交于 2019-12-01 11:57:47
I have a code in which the character array is populated by integers (converted to char arrays), and read by another function which reconverts it back to integers. I have used the following function to get the conversion to char array: char data[64]; int a = 10; std::string str = boost::lexical_cast<std::string>(a); memcpy(data + 8*k,str.c_str(),sizeof(str.c_str())); //k varies from 0 to 7 and the reconversion back to characters is done using: char temp[8]; memcpy(temp,data+8*k,8); int a = atoi(temp); This works fine in general, but when I try to do it as part of a project involving qt (ver 4.7

atoi on a character array with lots of integers

北城以北 提交于 2019-12-01 10:39:15
问题 I have a code in which the character array is populated by integers (converted to char arrays), and read by another function which reconverts it back to integers. I have used the following function to get the conversion to char array: char data[64]; int a = 10; std::string str = boost::lexical_cast<std::string>(a); memcpy(data + 8*k,str.c_str(),sizeof(str.c_str())); //k varies from 0 to 7 and the reconversion back to characters is done using: char temp[8]; memcpy(temp,data+8*k,8); int a =

Output of a C string program

半腔热情 提交于 2019-11-28 14:11:10
#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) which indicates printf skips s[4]. here is a ideone link for the same: http://ideone.com/UUQxb2 Explain

Comparing character arrays and string literals in C++

删除回忆录丶 提交于 2019-11-28 13:47:23
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. std::strcmp returns 0 if strings are equal. Check the documentation for strcmp. Hint: it doesn't return a boolean value. ETA:

PHP - iterate on string characters

社会主义新天地 提交于 2019-11-28 04:36:19
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 function stringToArray($s) { $r = array(); for($i=0; $i<strlen($s); $i++) $r[$i] = $s[$i]; return $r; }