Retrieve a char from a char array - objective-c

Does anyone know how to retrieve a char value from a char array:
char* alphaChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int rowIndex = 0; rowIndex < 12; rowIndex++)
{
char* text = (char*)alphaChars[0]; //this throws an error
//CGContextShowTextAtPoint(context, 10, 15, text, strlen(text)); //This is where I wanna use it
}

If you just want one character, you don't want to assign it to a pointer:
char text = alphaChars[0];
Then you would call your next function:
CGContextShowTextAtPoint(context, 10, 15, &text, 1);
If you want the whole string, which is sort of what your code looks like it's doing, you don't need to have an intermediate variable at all.

Related

Force decimal places to a string

How can I add a trail of six 0 of decimal places to a number in string type?
I tried PadRight but didn't get the result that I wanted. For example:
125 ===to===> 125,000000
14,5 ===to===> 14,500000
so on..
Is there any function in VB.NET can do that or do I need to split the string then combine them?
I'm not completely sure what you're asking, but if you want to add six 0's to a string then I would make a function, something like this:
private static string AddZeroes(string myString)
{
//Makes an array that is 6 elements higher than the original string
string[] myStringArray = new string[myString.Length + 6];
//Sets the array to have all elements of myString inside of it
for (int i = 0; i < myString.Length; i++)
{
myStringArray[i] = Convert.ToString(myString[i]);
}
//Adds the six 0's to the array
for (int i = myString.Length; i < myString.Length + 6; i++)
{
myStringArray[i] = "0";
}
//Sets myString to an empty string, then adds all the contents of the myStringArray to it
myString = "";
for (int i = 0; i < myStringArray.Length; i++)
{
myString += myStringArray[i];
}
//Returns the string that should now have six 0's added to the end
return myString;
}
If you wanted a comma or decimal, it shouldn't be too hard to just add one right before the for loop that adds the 0's, and also make sure that the array is the length of the string + 7 instead, to fit that extra character.

QStringList multiple entries and comparison

I am using QInputDialog::getText to allow the user to input a string. The user should enter an arbitrary number of comma separated integers.
Then I would like to check if there is a duplicate.
In order to convert the input to integers, I have tried the following:
Split the input into a list using the QString::split with comma as an argument
Iterate over the elements of the QStringList
for (int i = 1; i <= list.count(); i++)
{
list.at(i).toInt();
}
The loop leads to a crash.
How to fix this?
Cause
Because the element index in the list is zero-based, i.e. starts from 0 and runs up to count - 1, your loop tries to access a non-existing element with index count and your program crashes.
Solution
Either change
for (int i = 1; i <= list.count(); i++)
to
for (int i = 0; i < list.count(); i++)
or even better, use a ranged for
for (const QString &substr : list)
Then use QList::takeFirst() and QList::contains in a while loop to check for duplicates.
Example
Here is an example I have prepared for you:
#include <QApplication>
#include <QInputDialog>
#include <QMessageBox>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
const QString &str(QInputDialog::getText(nullptr, "Input integers",
"Enter comma separated integers."
" Do not use spaces."));
QList<int> numbers;
bool hasDuplicate = false;
for (const QString &substr : str.split(','))
numbers.append(substr.toInt());
while (numbers.count() && !hasDuplicate)
hasDuplicate = numbers.contains(numbers.takeFirst);
QMessageBox::information(nullptr, "Result",
hasDuplicate ? "There is at least one duplicate"
: "All numbers are unique");
return 0;
}
Result
For the example input of
11,22,33
this message is shown
For the example input of
11,22,33,22
this message is shown

How to covert Textbox to integer array in c++/cli

I'm trying to convert the textbox to an integer array assuming that every character of the textbox is a digit.
//textbox is named input
int size = this->input->Text->Length;
int * num = new int[size];
int Counter = 0;
//for loop used since textbox inputs like a calculator
//Ex: the number: 234 is inputed: 2, then 23, then 234
for (int i = size; i > 0; i--)
{
num2[Counter] = System::Convert::ToInt32(input->Text[i-1]);
Counter += 1;
}
Array of numbers should be:
num[0] = 4, num[1] = 3, num[2] = 2
Upon research though it seems that it's finding the integer unicode value instead.
Code input->Text[i-1] returns a single Unicode character value of the wchar_t type. That is implicitly cast to Int32, i.e. the symbol code.
You have to convert the char to a string, before converting to the number. You can use the Substring method or the ToString method for this purpose.
You can do it as follows:
String^ text = this->input->Text;
int size = text->Length;
int * num = new int[size];
for (int i = size - 1; i >= 0; i--) {
num[i] = Convert::ToInt32(text->Substring(size - i - 1, 1));
}
However, you should not mix managed and unmanaged code.
There is a better way. Use a generic collection instead of an array.
String^ text = this->input->Text;
int size = text->Length;
List<int>^ nums = gcnew List<int>();
for (int i = size - 1; i >= 0; i--) {
nums->Add(Convert::ToInt32(text[i].ToString()));
}
Don't forget
using namespace System::Collections::Generic;
The list can be accessed by index like an array:
nums[i]
So it is convenient to work with. And most importantly, do not need to worry about freeing memory.

c and objective-c -- const char* and char*

I have a function:
-(void)ADPCMDecode:(char *)indata : (short *)outdata :(long)len {
indata is a char and the function does pointer arithmetic to iterate for a length of len, modifying outdata, which is a short and I will need to do pointer arithmetic to get the values from it.
I am attempting to call the function using:
const char *modulatedBytes1 = [modulatedAudio bytes];
char *modulatedBytes [] = modulatedBytes1;
unsigned int moduleatedLength = [modulatedAudio length];
short *decompressedBytes = NULL;
[self ADPCMDecode:modulatedBytes :decompressedBytes :moduleatedLength];
DLog(#"%hi",decompressedBytes[1]);
I get a BAD ACCESS error on this line: *outp++ = valprev; within the function, because I am passing a constant char * instead of a char *
How should I call the function, and how would I get the output from it?
I have no background in C, which is why I do not understand how to go about doing this.
Here is the C only version of the same question:
https://pastee.org/d3y3z

XOR reverse a string in objective-c get an error

I want to use the following code to reverse a char * type string in objective-c:
- (char *)reverseString:(char *)aString
{
unsigned long length = strlen(aString);
int end = length - 1;
int start = 0;
while (start < end) {
aString[start] ^= aString[end];
aString[end] ^= aString[start];
aString[start] ^= aString[end];
++start;
--end;
}
return aString;
}
But I got an error EXC_BAD_ACCESS at this line
aString[start] ^= aString[end]
I googled and found people said I can't modify a literal string because it is readonly. I am new to C so I wonder what simple data type (no object) I can use in this example? I get the same error when I use (char []) aString to replace (char *) aString.
I assume you're calling this like
[myObj reverseString:"foobar"];
The string "foobar" here is a constant literal string. Its type should be const char *, but because C is braindead, it's char *. But it's still constant, so any attempt to modify it is going to fail.
Declaring the method as taking char[] actually makes no difference whatsoever. When used as a parameter type, char[] is identical to char*.
You have two choices here. The first is to duplicate the string before passing it to the method. The second is to change the method to not modify its input string at all but instead to return a new string as output. Both can be accomplished using strdup(). Just remember that the string returned from strdup() will need to be free()'d later.