Getting char from array of strings [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have an array of strings and an array of ints. Usually when I want to get a certain char from string I use:
string[char_position];
But when I have two arrays the same way of getting char doesn't work:
string[i][char_position[i]];
How can I get certain char from array of strings?

Try it like this instead:
string[i][char_position];
You sholdn't be subscripting char_position.

In first example you are using char_position as int but in second you are using it as array. Either one of this is wrong obviously if you mean same variable in two cases. But what it looks like you want to access jth charecter of ith string. If it is that then
string[i][j]; // j is position of char in string[i]

string[i][char_position[i]]; means the instructions:
get the INTEGER from char_position array indexed at i
get the character in ith string, in the position got from the first step
This is valid and correct ONLY if char_position is an ARRAY and not a SCALAR VARIABLE.
On the other hand, if char_position IS a scalar variable THEN:
string[i][char_position] is the way to retrieve char_positionth character in the ith string, in the array of character arrays string

Related

String Encode in C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I need to convert the string "Test €" to "Test &#8364 ;" and vice versa. Please make a note that its a string and not from the xml. For your information I am developing the application in C++ and using Xerces also for XML Parsing. Please help me how it can be achieved in c++ application.
Thanks,
Ram
I think this answer may be platform-dependent, though I don't know for sure.
You can use stringstreams and casting. If lookup is a string holding the decimal version of the character code, this function will return the character version:
char fixchar(string lookup){
stringstream converter (lookup);
int i;
converter >> dec >> i;
return (char)i
(Note that for hex strings, which are prefixed with #x instead of #, you can just use hex instead of dec).
You can get the lookup strings by using the find function on the original string. Here's a loop that uses the above function to convert a string (called fixd) with &#x[number] substrings into a normal string with no character codes:
while (fixd.find("&#x")!=string::npos){
tag = int(fixd.find("&#"));
endtag = int(fixd.find(";"));
fixd = fixd.substr(0,tag) + fixchar(fixd.substr(tag+3,endtag-tag-3)) + fixd.substr(endtag+1, fixd.length()-tag-4);
}
Similarly, you should be able to get the int version of a character just by casting it, after which you can do whatever you want with it, including adding it in decimal form to a string.

Function to choose a pivot for quick sort [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
int choose_pivot(int lo, int hi){
int mid = ((hi-lo)/2)+1;
if((arr[hi]<arr[lo])^(arr[hi]<arr[mid])){
return hi;
}
else if((arr[lo]<arr[hi])^(arr[lo]<arr[mid])){
return lo;
}
else{
return mid;
}
}
This function should choose the pivot as follows. Consider the first, middle, and final elements of the given array. (If the array has odd length it should be clear what the "middle" element is; for an array with even length 2k, use the kth element as the "middle" element. Identify which of these three elements is the median. Return the its value.
Is there anything wrong with this function which may not let it do what it is meant for? Am I missing some case for which the function doesn't give desired result?
If you are trying to solve task from coursera.org, you need to use
int mid = ((hi-lo)/2);
There is one thing that I would consider a fatal error: there's
no documentation with regards to pre-conditions. In particular,
is calling it with lo == hi legal or not. (If it's legal, the
code has undefined behavior.) And what do lo and hi
represent? Under the usual C++ conventions, which I find work
very well, lo would be inclusive, and hi exclusive. But
again, you don't tell us, despite the fact that it is important
to know if we are to judge the correctness of the code. (The
fact that you do arr[hi] makes me think that you've adopted
the somewhat unusual convention of making both ends inclusive.)
Without such information, it's impossible to do anything but
make stylistic comments (e.g. use != rather than ^ for
comparing results, use return with ?: rather than if ... else
if ... else, etc.).
You should probably use != instead of ^ to compare boolean results for inequality.
Based on the textual description, you should be returning arr[hi] instead of hi (and similar for mid, lo).

Does the cin function add null terminated at the end of input? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
If I declare a character array: char arr[200]
and then I subsequently use the function cin to read values into arr[200]
and I type into the command window line: abcd
Is there a null terminated: \0 automatically added to the array at the end of the input?
(I don't think so because I tested it using the cin function: cin>>abcd )
Can somebody explain it to me why?
Below is a snippet of my code I use to test
char arr[200]
int count=0;
int i=0;
cin>>arr // i type into command window:abcd
while (arr[i] != '\0')
{
count++;
i++
}
My count value will not be 4 but like 43 hence I concluded that the character array is not null terminated after the cin function
Formatted input from a std::istream into a character array will null-terminate the input, as specified in C++11 27.7.2.2.3/9:
operator>> then stores a null byte (charT()) in the next position
The code you've posted gives the expected result once the obvious syntax errors are fixed. But beware that this is very dangerous; there is no check on the length of the array, so too much input will overflow it. I strongly recommend you use the std::string class, rather than plain character arrays, for managing strings.
The code you posted in a comment via a link looks like this:
char array[20];
int length=getlength(array);
cin>>array;
reading into the array after attempting to measure the string length of the uninitialised array. This could give any result, or crash, or cause any other example of undefined behaviour.
In future, you should make sure that the code you post in your question is the same code that exhibits the behaviour you're asking about; otherwise, it's impossible to answer the question.
Yes, the input will be zero-terminated. Otherwise you wouldn't be able to, for example, print it without printing random characters after your input.

Search for all substings between "+" and "n" characters [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm trying to change some equations from input file to a "readable" form.
Currently I need to read all numerical values (as substrings, will convert them to int later) within a string P. All the values I'm interested with are between "+" and "n" characters (in this order for one loop, and the inverted order for other loop).
I need a loop which finds all of them and save them to array (of unknown size, since I don't know how long the string P will be).
Input examples (strings P in quotes):
"+n2+-n"
First loop (reads from + to n) so substrings C are: "", "-"
The second loop (reads from n to +) so substrings E are: "2", ""
"+2n3+3n2+n"
First loop: "2", "3", ""
Second loop: "3", "2", ""
"+-n14+-11n+1"
First loop: "-", "-11"
Second loop: "14", ""
I could add "+" to the end of the P string if solution requires.
ps. If someone's have an idea how to extract a constant from the end of string seen in example 3 (+1, or any other) I would really appreciate. The hard thing is I cannot tell how long it'll be (can by +1 can be -300000 or so).
Please consider to take a look at regular expressions (in general) and the new std::regex class from c++0x (in particular).
C++0x: Regular Expressions
Regular expressions are always a elegant solution if you want to parse any more complex patterns.
I didn't really understood your question, but what I personally use whenever i have unknown-length / extremely big inputs is a linked list , that you could implement in C++ with dynamic memory allocation , which is also possible in C with malloc(), if you ever find that you need C syntax.
As with the parsing of the input, you can use a variable initialized with 1, and you just multiply it with -1 every time you get to the end of a substring, and use simple if statement that covers each case. Or you could use a char that takes either "n" or "+", and so you have to make the if statement cover only a few lines of code, after you're done with the actual parsing, and whenever your cursor gets to the above-mentioned char variable, it acts like it's done it's job for another substring.

difference between way a[i] and *(a+1) are handled [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
file 1:
int mango[100];
file 2:
extern int *mango;
...
/* some code that references mango[i] */
When both are accessed as *(mango+i) why do we get error?
Also if char mango[5]; then array starts at location mango such that mango=&mango[0]. So variable 'mango' contains address to itself or the first character of array?
If you define a variable a such as:
char a[5];
Then you have defined an array of five (5) characters. The address of the first character, which can be accessed via a[0] or simply *a, is named "a". The array is also called "a" because the array is named after the address of its first element, as given in the array declaration by the programmer.
It is not possible to change the address of a by assigning to a. If you attempt to write:
a = NULL;
The error you receive should indicate that an lvalue (value which may appear left of an assignment operator) is required.
The variable a represents an array of characters, and as-such, it is a variable that is bound to the address that holds the value for the first character of the array. When array-names are passed as arguments to functions, or used on the right-hand side of the assignment operator, they decay (i.e., are implicitly converted) into pointers to the first element of the array. Thus arrays are not pointers, but because of the implicit conversion, they can be used like pointers in many C and C++ operations.
That being said, if you are defining char a[4]; in one .c file, and then including that declaration in another .c file that has a variable called char* a, then the C-compiler is going to complain about multiple definitions of the same variable.