get random string with limit 10 maximum ios? - iphone

am generating random string it contains alpha numberic values. The thing is i need to set exactly 10 digits to store in a particular string sometime i getting 10 digits exactly but most of the time i am getting 4,5,7, or even 1 character values :
here my sample code :
NSString *alphabet = #"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY0123456789";
NSMutableString *s = [NSMutableString stringWithCapacity:10];
for (NSUInteger i = 0; i < 10; i++) {
u_int32_t r = arc4random() % [alphabet length];
unichar c = [alphabet characterAtIndex:r];
[s appendFormat:#"%C", c];
NSLog(#"%#",s);
}
my nslog :
y
yC
yCD
yCDC
yCDCd
yCDCdP
yCDCdP1
yCDCdP1F
yCDCdP1Fg
yCDCdP1Fgq

There's nothing wrong with the code.
Just, put NSLog(#"%#",s); right out of the for loop.

You have put your NSLog inside the for loop. That might make you think it's wrong. Take
NSLog(#"%#",s);
out of the loop.

Related

Convert NSNumber to hex string with 16 digits

I have to convert an NSNumber to a hex string like follows:
return [NSString stringWithFormat:#"%llX", self.unsignedLongLongValue];
Unfortunately, this will sometimes give me string like 93728A166D1A287 where they should be 093728A166D1A287, depending on the number.
Hint: the leading 0.
I've also tried it with:
return [NSString stringWithFormat:#"%16.llX", self.unsignedLongLongValue];
without success.
I could do something like this, but that just sucks:
- (NSString *)hexValue {
NSString *hex = [NSString stringWithFormat:#"%llX", self.unsignedLongLongValue];
NSUInteger digitsLeft = 16 - hex.length;
if (digitsLeft > 0) {
NSMutableString *zeros = [[NSMutableString alloc] init];
for (int i = 0; i < digitsLeft; i++) [zeros appendString:#"0"];
hex = [zeros stringByAppendingString:hex];
}
return hex;
}
So finally my question, is there a way to enforce the string to be 16 characters?
If you need to zero-pad your hex numbers, use zero in front of the format specifier, like this:
return [NSString stringWithFormat:#"%016llX", self.unsignedLongLongValue];
This should take care of formatting your number with 16 digits, regardless of how many "meaningful" digits the number has.
Here is a demo of this format string in plain C (this part is shared between the two languages).
Use:
return [NSString stringWithFormat:#"%016llX", self.unsignedLongLongValue];
Which sets leading 0 and the length of the output string.

Counting same letters in a word

I need to count same letters in a word. For example: apple is my word and first I found whether 'a' exists in this letter or not. After that I want to count the number of 'a' in that word but I couldn't do that. This is my code which finds the specific letter;
if([originalString rangeOfString:compareString].location==NSNotFound)
{
NSLog(#"Substring Not Found");
}
else
{
NSLog(#"Substring Found Successfully");
}
originalString is a word which I took from my database randomly. So, how to count?
Thanks for your help.
i have different idea let's try...
NSString *string = #"appple";
int times = [[string componentsSeparatedByString:#"p"] count]-1;
NSLog(#"Counted times: %i", times);
NSString *strComplete = #"Appleeeeeeeeeeeeeee Appleeeeeeeee Aplleeeeeeeeee";
NSString *stringToFind = #"e";
NSArray *arySearch = [strComplete componentsSeparatedByString:stringToFind];
int countTheOccurrences = [arySearch count] - 1;
Output :
countTheOccurrences --- 34
You could just loop over the string once, adding each letter to an NSMutableDictionary (as the key) and keeping a tally of how many times the letter occurs (as the value).
The resulting NSMutableDictionary would hold the number of occurences for each unique letter, whereby you can extract what you like.

Divide NSInteger with int

I am trying to divide NSInteger with some number, but I am receiving errors.
This is what I am trying to do:
length = [corner count];
for (int i=0; i<length; i++) {
if ([resultDate rangeOfString:[corner objectAtIndex:i]].location != NSNotFound) {
cornerResult++;
}
}
cornerResultLabel.text = [NSString stringWithFormat:#"%i", cornerResult];
This works as it should.. I search through the array and count the results and write it.
But, I need cornerResult divided with 4. When I add cornerResult / 4 it shows me the error(as I wrote in comment). I have no idea why is this making problem.
you probably meant to divide the intValue of the NSNumber, not the NSNumber* itself:
int num = number.intValue;
int result = num / 4;
(full code sample and error message would help)
You are dividing an int which becomes a float , use %f instead.
I fix it this way. First I defined int and after that I set NSInteger value to int and after that divide.
int helper = cornerResult;
helper = helper / 4;

Why is my NSRange always 0?

I want to implement a delete key for my calculator app. My pseudocode for this was:
foo = length of current number
bar = length of current number-1
current number = current number with character at index (foo-bar) removed
To do this in objective-c I have tried to implement this using the NSRange function as well as the StringbyappendingString method:
- (IBAction)DelPressed {
if ([self.display.text length] >=2)
{
int len = [self.display.text length];//Length of entire display
//Add to the brainlong printing out the length of the entire display
self.brainlog.text = [NSString stringWithFormat:#"Len is %g",len];
int le = ([self.display.text length]-1);//Length of entire display - 1
NSString *lestring = [NSString stringWithFormat:#"LE is %g",le];
//Add to the brainlog printing out the length of the display -1
self.brainlog.text = [self.brainlog.text stringByAppendingString:lestring];
NSRange range = NSMakeRange(le, len);//range only includes the last character
self.display.text = [self.display.text stringByReplacingCharactersInRange:range withString:#" "];//Replace the last character with whitespace
}
The output to brainlog (ie the length of both le and len) is:
Len is -1.99164 Le is -1.99164
Hard to see, but here's an image of the number I input to the calculator using the iOS simulator:
I have tried to change the value of le (ie making it the length of the display -3 or -5 etc) and both le and len are still the same.
I have also tried to make le in reference to len:
int len = [self.display.text length];//Length of entire display
//Add to the brainlong printing out the length of the entire display
self.brainlog.text = [NSString stringWithFormat:#"Len is %g",len];
int le = len-1;
But the values of the two are still the same. How can I use NSRange to delete the last character of the display?
Edit: Using Dustin's fix, I now have reduced a rather lengthy function into 6 lines of code:
-(IBAction)DelPressed{
if ([self.display.text length] >=2)
{
self.display.text = [self.display.text substringToIndes:[self.display.text length] -1];
}
}
Use substring instead; it's much better if all you want to do is remove the last character.
More specifically, substringToIndex(/*length-1*/)
Check this reference out if you need to do other things with strings

Objective-C += equivalent?

Sorry for the newbie question, but i cannot find an answer to it.
I have a simple operation. I declare a variable, and then i want to loop through an array of integers and add these to the variable. However, i can't seem to find how to get a += equivalent going in Objective C.
Any help would be awesome.
Code:
NSInteger * result;
for (NSInteger * hour in totalhours)
{
result += hour;
}
NSInteger is not a class, it's a typedef for int. You cannot put it into collections like NSArray directly.
You need to wrap your basic data types (int, char, BOOL, NSInteger (which expands to int)) into NSNumber objects to put them into collections.
NSInteger does work with +=, keep in mind that your code uses pointers to them, which is probably not what you want anyway here.
So
NSInteger a = 1, b = 2;
a += b;
would work.
If you put them with [NSNumber numberWitInt:a]; etc. into an NSArray, this is not that easy and you need to use -intValue methods to extract their values first.
If totalhours actually contains NSNumber objects you need the following:
NSInteger result = 0;
for(NSNumber* n in totalhours)
{
result += [n integerValue];
}
The problem is that you are confusing NSInteger (a typedef for int or long) with a class instance such as NSNumber.
If your totalhours object is an array of NSNumber objects, you'll need to do:
NSInteger result;
for (NSNumber *hour in totalhours)
{
result += [hour integerValue];
}
No problem using the '+=' operator, just be sure about the objects you are working with...
Your code might be :
NSNumber *n; NSUInteger t = 0;
for(n in totalHours) {
t += [n integerValue];
}
// you got your total in t...
The += operation definitly works. All you need to do is initialize your result variable so it has a start value.
E.g. NSInteger * result = 0;
Good luck!
Your problem is probably that you're using a pointer to an NSInteger instead of an actual NSInteger. You're also not initializing it. Try this:
NSInteger result = 0;
for (NSInteger * hour in totalhours)
{
result += *hour;
}