I am trying to replace multiple character found in a string at once not using stringByReplacingOccurrencesOfString. So if my string is: #"/BKL_UO+C-" I what to change / to _ ; - to +; + to -; _ to /.
In my code I have tried to do this by applying for each of the signs that I want to replace first the stringByReplacingOccurrencesOfString and save it as a new string and created an NSArray of chars with them. Compared the differences and save them in another array where I push all the differences from all 4 arrays. Then apply all differences to the initial string.
Initial hash string is: #"lRocUK/Qy+V2P3yDhCd74RvHjCDzlTfrGMolZZE0pcQ"
Expected result: #"lRocUK_Qy-V2P3yDhCd74RvHjCDzlTfrGMolZZE0pcQ"
NSMutableArray *originalHashArrayOfCharFromString = [NSMutableArray array];
for (int i = 0; i < [hash length]; i++) {
[originalHashArrayOfCharFromString addObject:[NSString stringWithFormat:#"%C", [hash characterAtIndex:i]]];
}
//1 change
NSString *backslashRplecedInHashString = hash;
backslashRplecedInHashString = [backslashRplecedInHashString stringByReplacingOccurrencesOfString:#"/" withString:#"_"];
NSMutableArray *hashConvertBackslashToUnderline = [NSMutableArray array];
for (int i = 0; i < [hash length]; i++) {
[hashConvertBackslashToUnderline addObject:[NSString stringWithFormat:#"%C", [backslashRplecedInHashString characterAtIndex:i]]];
}
//2 change
NSString *minusRplecedInHashString = hash;
minusRplecedInHashString = [minusRplecedInHashString stringByReplacingOccurrencesOfString:#"-" withString:#"+"];
NSMutableArray *hashConvertMinusToPlus = [NSMutableArray array];
for (int i = 0; i < [hash length]; i++) {
[hashConvertMinusToPlus addObject:[NSString stringWithFormat:#"%C", [minusRplecedInHashString characterAtIndex:i]]];
}
//3 change
NSString *underlineRplecedInHashString = hash;
underlineRplecedInHashString = [underlineRplecedInHashString stringByReplacingOccurrencesOfString:#"_" withString:#"/"];
NSMutableArray *hashConvertUnderlineToBackslash = [NSMutableArray array];
for (int i = 0; i < [hash length]; i++) {
[hashConvertUnderlineToBackslash addObject:[NSString stringWithFormat:#"%C", [underlineRplecedInHashString characterAtIndex:i]]];
}
//4 change
NSString *plusRplecedInHashString = hash;
plusRplecedInHashString = [plusRplecedInHashString stringByReplacingOccurrencesOfString:#"+" withString:#"-"];
NSMutableArray *hashConvertPlusToMinus = [NSMutableArray array];
for (int i = 0; i < [hash length]; i++) {
[hashConvertPlusToMinus addObject:[NSString stringWithFormat:#"%C", [plusRplecedInHashString characterAtIndex:i]]];
}
NSMutableArray *tempArrayForKey = [[NSMutableArray alloc] init];
NSMutableArray *tempArrayForValue = [[NSMutableArray alloc] init];
int possitionInTempArray = 0;
//Store all changes of original array in a temp array
//1 replace
for (int countPosssition = 0 ; countPosssition < [hash length]; countPosssition++) {
if ([originalHashArrayOfCharFromString objectAtIndex:countPosssition] != [hashConvertBackslashToUnderline objectAtIndex:countPosssition]) {
[tempArrayForValue addObject:[hashConvertBackslashToUnderline objectAtIndex:countPosssition]];
[tempArrayForKey addObject:[NSNumber numberWithInt:countPosssition]];
possitionInTempArray++;
}
}
//2 replace
for (int countPosssition = 0 ; countPosssition < [hash length]; countPosssition++) {
if ([originalHashArrayOfCharFromString objectAtIndex:countPosssition] != [hashConvertMinusToPlus objectAtIndex:countPosssition]) {
[tempArrayForValue addObject:[hashConvertMinusToPlus objectAtIndex:countPosssition]];
[tempArrayForKey addObject:[NSNumber numberWithInt:countPosssition]];
possitionInTempArray++;
}
}
//3 replace
for (int countPosssition = 0 ; countPosssition < [hash length]; countPosssition++) {
if ([originalHashArrayOfCharFromString objectAtIndex:countPosssition] != [hashConvertUnderlineToBackslash objectAtIndex:countPosssition]) {
[tempArrayForValue addObject:[hashConvertUnderlineToBackslash objectAtIndex:countPosssition]];
[tempArrayForKey addObject:[NSNumber numberWithInt:countPosssition]];
possitionInTempArray++;
}
}
//4 replace
for (int countPosssition = 0 ; countPosssition < [hash length]; countPosssition++) {
if ([originalHashArrayOfCharFromString objectAtIndex:countPosssition] != [hashConvertPlusToMinus objectAtIndex:countPosssition]) {
[tempArrayForValue addObject:[hashConvertPlusToMinus objectAtIndex:countPosssition]];
[tempArrayForKey addObject:[NSNumber numberWithInt:countPosssition]];
possitionInTempArray++;
}
}
NSLog(tempArrayForKey.debugDescription);
NSLog(tempArrayForValue.debugDescription);
// use the them array to submit changes
for (int count = 0; count < tempArrayForKey.count; count++) {
[originalHashArrayOfCharFromString setObject:[tempArrayForValue objectAtIndex:count] atIndexedSubscript:(int)[tempArrayForKey objectAtIndex:count]];
}
[hash setString:#""];
//Reassemble the hash string using his original array that sufferet modificvations
for (int count = 0; count<originalHashArrayOfCharFromString.count; count++) {
[hash appendString:[NSString stringWithFormat:#"%#", [originalHashArrayOfCharFromString objectAtIndex:count]]];
}
NSLog(hash);
What if you temporarily swapped out the occurrences to a "temp" character so you wouldn't lose data on the swaps and do something like this:
NSString * initialString = #"lRocUK/Qy+V2P3yDhCd74RvHjCDzlTfrGMolZZE0pcQ";
initialString =[initialString stringByReplacingOccurrencesOfString:#"/" withString:#"^"];
initialString =[initialString stringByReplacingOccurrencesOfString:#"-" withString:#"*"];
initialString =[initialString stringByReplacingOccurrencesOfString:#"+" withString:#"-"];
initialString =[initialString stringByReplacingOccurrencesOfString:#"*" withString:#"+"];
initialString =[initialString stringByReplacingOccurrencesOfString:#"_" withString:#"/"];
initialString =[initialString stringByReplacingOccurrencesOfString:#"^" withString:#"_"];
Related
I have a string like #"1234123412341234", i need to append space between every 4 chars like.
#"1234 1234 1234 1234"
i.e, I need a NSString like Visa Card Type. I have tried like this but i didn't get my result.
-(void)resetCardNumberAsVisa:(NSString*)aNumber
{
NSMutableString *s = [aNumber mutableCopy];
for(int p=0; p<[s length]; p++)
{
if(p%4==0)
{
[s insertString:#" " atIndex:p];
}
}
NSLog(#"%#",s);
}
Here's a unicode aware implementation as a category on NSString:
#interface NSString (NRStringFormatting)
- (NSString *)stringByFormattingAsCreditCardNumber;
#end
#implementation NSString (NRStringFormatting)
- (NSString *)stringByFormattingAsCreditCardNumber
{
NSMutableString *result = [NSMutableString string];
__block NSInteger count = -1;
[self enumerateSubstringsInRange:(NSRange){0, [self length]}
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
if ([substring rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]].location != NSNotFound)
return;
count += 1;
if (count == 4) {
[result appendString:#" "];
count = 0;
}
[result appendString:substring];
}];
return result;
}
#end
Try it with this test string:
NSString *string = #"ab 😗😌 132487 387 e e e ";
NSLog(#"%#", [string stringByFormattingAsCreditCardNumber]);
The method works with non-BMP characters (i.e. emoji) and handles existing white space.
Your code is pretty close, however a better semantic for the method is to return a new NSString for any given input string:
-(NSString *)formatStringAsVisa:(NSString*)aNumber
{
NSMutableString *newStr = [NSMutableString new];
for (NSUInteger i = 0; i < [aNumber length]; i++)
{
if (i > 0 && i % 4 == 0)
[newStr appendString:#" "];
unichar c = [aNumber characterAtIndex:i];
[newStr appendString:[[NSString alloc] initWithCharacters:&c length:1]];
}
return newStr;
}
You should do like this:
- (NSString *)resetCardNumberAsVisa:(NSString*)originalString {
NSMutableString *resultString = [NSMutableString string];
for(int i = 0; i<[originalString length]/4; i++)
{
NSUInteger fromIndex = i * 4;
NSUInteger len = [originalString length] - fromIndex;
if (len > 4) {
len = 4;
}
[resultString appendFormat:#"%# ",[originalString substringWithRange:NSMakeRange(fromIndex, len)]];
}
return resultString;
}
UPDATE:
You code will be right on the first inserting space charactor:
This is your originalString:
Text: 123412341234
Location: 012345678901
Base on your code, on the first you insert space character, you will insert at "1" (with location is 4)
And after that, your string is:
Text: 1234 12341234
Location: 0123456789012
So, you see it, now you have to insert second space charater at location is 9 (9%4 != 0)
Hope you can fix your code by yourself!
The code snippet from here do what do you want:
- (NSString *)insertSpacesEveryFourDigitsIntoString:(NSString *)string
andPreserveCursorPosition:(NSUInteger *)cursorPosition
{
NSMutableString *stringWithAddedSpaces = [NSMutableString new];
NSUInteger cursorPositionInSpacelessString = *cursorPosition;
for (NSUInteger i=0; i<[string length]; i++) {
if ((i>0) && ((i % 4) == 0)) {
[stringWithAddedSpaces appendString:#" "];
if (i < cursorPositionInSpacelessString) {
(*cursorPosition)++;
}
}
unichar characterToAdd = [string characterAtIndex:i];
NSString *stringToAdd =
[NSString stringWithCharacters:&characterToAdd length:1];
[stringWithAddedSpaces appendString:stringToAdd];
}
return stringWithAddedSpaces;
}
swift3 based on Droppy
func codeFormat(_ code: String) -> String {
let newStr = NSMutableString()
for i in 0..<code.characters.count {
if (i > 0 && i % 4 == 0){
newStr.append(" ")
}
var c = (code as NSString).character(at: i)
newStr.append(NSString(characters: &c, length: 1) as String)
}
return newStr as String
}
Please make sure that your string length should times by 4.
This solution will insert on the right hand side first.
- (NSString*) fillWhiteGapWithString:(NSString*)source
{
NSInteger dl = 4;
NSMutableString* result = [NSMutableString stringWithString:source];
for(NSInteger cnt = result.length - dl ; cnt > 0 ; cnt -= dl)
{
[result insertString:#" " atIndex:cnt];
}
return result;
}
I need to get the highest repeated character in string and the count of the repeated character.
For that i stored the each character of the string in the array and using the for loops i got each character and the count. is there any other delegate methods to find it to reduce the code?
for example
NSRange theRange = {0, 1}; //{location, length}
NSMutableArray * array = [NSMutableArray array];
for ( NSInteger i = 0; i < [myFormattedString length]; i++) {
theRange.location = i;
[array addObject:[myFormattedString substringWithRange:theRange]];
}
int countForChar = 0;
for (int i=0; i<[array count]; i++) {
NSString *firstCharacter = [array objectAtIndex:i];
for (int j=1; j< [array count]; j++) {
if ([firstCharacter isEqualToString:[array objectAtIndex:j]]) {
countForChar = countForChar + 1;
}
}
NSLog(#"The Charcter is %# The count is %d", firstCharacter, countForChar);
countForChar = 0;
}
Thanks in advance...
Because the string may have more than a char have same most repeat count, so here is my solution:
- (NSArray *)mostCharInString:(NSString *)string count:(int *)count{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
int len = string.length;
NSRange theRange = {0, 1};
for (NSInteger i = 0; i < len; i++) {
theRange.location = i;
NSString *charStr = [string substringWithRange:theRange];
int preCount = 0;
if ([dict objectForKey:charStr]) {
preCount = [[dict objectForKey:charStr] unsignedIntegerValue];
}
[dict setObject:#(preCount+1) forKey:charStr];
}
NSArray *sortValues = [[dict allValues] sortedArrayUsingSelector:#selector(compare:)];
*count = [[sortValues lastObject] unsignedIntegerValue];
return [dict allKeysForObject:#(*count)];
}
How to use and test:
int mostRepeatCount = 0;
NSArray *mostChars = nil;
mostChars = [self mostCharInString:#"aaabbbcccc" count:&mostRepeatCount];
NSLog(#"count:%d char:%#", mostRepeatCount, mostChars);
the result is:
count:4 char:(
c
)
try:
mostChars = [self mostCharInString:#"aaabbbccccdddd" count:&mostRepeatCount];
the result is:
count:4 char:(
d,
c
)
Hope to help you.
Here is my code might be not good enough but I think its the fastest
NSString *myFormattedString = #"oksdflajdsfd";
NSMutableDictionary *lettersCount = [[NSMutableDictionary alloc] init];
for (NSInteger i = 0; i < [myFormattedString length]; i++) {
unichar charAtIndex = [myFormattedString characterAtIndex:i];
NSNumber *countForThisChar = [lettersCount objectForKey:[NSString stringWithFormat:#"%c",charAtIndex]];
int count = 1;
if(countForThisChar) {
count = [countForThisChar integerValue] + 1;
[lettersCount setObject:#(count) forKey:[NSString stringWithFormat:#"%c",charAtIndex]];
} else {
// not added yet, add it with 1 count
[lettersCount setObject:#(count) forKey:[NSString stringWithFormat:#"%c",charAtIndex]];
}
}
// for now the work is O(n)
// ignoring the work of this cycle or consider it as O(1)
NSString *mostFrequentChar = nil;
NSInteger maxCount = 0;
for(NSString *oneChar in lettersCount.keyEnumerator) {
NSNumber *count = [lettersCount objectForKey:oneChar];
if([count integerValue] > maxCount) {
mostFrequentChar = oneChar;
maxCount = [count integerValue];
}
}
NSLog(#"the char %# met for %d times", mostFrequentChar, maxCount);
Remember the search for an object in NsDictionary is O(1) for the average case scenario.
Here is an example that would work correctly with any string and has linear time complexity. This uses the NSCountedSet which can be pretty useful.
NSString* string = #"This is a very wonderful string. Ølsen & ジェイソン";
NSCountedSet* characterCounts = [[NSCountedSet alloc] init];
// This ensures that we deal with all unicode code points correctly
[string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[characterCounts addObject:substring];
}];
NSString* highestCountCharacterSequence = nil;
NSUInteger highestCharacterCount = 0;
for (NSString* characterSequence in characterCounts) {
NSUInteger currentCount = [characterCounts countForObject:characterSequence];
if (currentCount > highestCharacterCount) {
highestCountCharacterSequence = characterSequence;
highestCharacterCount = currentCount;
}
}
NSLog(#"Highest Character Count is %# with count of %lu", highestCountCharacterSequence, (unsigned long)highestCharacterCount);
Sadly, my silly example string ends up having space characters as the most repeated :)
Every character can be presented by its int value. Make an instance of NSArray with n size (n number of unique characters string can have). Loop through string and add +1 on (int)character index in array at every cycle. When you finish the character with greatest value in array is the highest repeated character.
I have an
NSString *str=#"123456789123456789123456";
My new string should be
NSString *newStr =#"1234 5678 9123 4567 8912 3456";
Can any one help me
Thanks in Advance
You can use this..
NSMutableArray *subStrings = [NSMutableArray array];
NSRange range = {0,subStrLength};
for(int i=0;i< [str length]; i+= subStrLength)
{
range.location = i;
[subStrings addObject:[str substringWithRange:range];
}
Update: NSString *strWithSpace = [subStrings componentsJoinedByString:#" "];
NSString *str=#"123456789123456789123456";
NSMutableString *mutableStr = [str mutableCopy];
for (int i = 4; i < mutableStr.length; i += 5) {
[mutableStr insertString:#" " atIndex:i];
}
How to Generate a random non-repeated(without repeating same alphabet) alphanumeric string from a given String in ios?
The following function will take a string and randomise it, usually each character from the input string only once:
- (NSString *)randomizeString:(NSString *)str
{
NSMutableString *input = [str mutableCopy];
NSMutableString *output = [NSMutableString string];
NSUInteger len = input.length;
for (NSUInteger i = 0; i < len; i++) {
NSInteger index = arc4random_uniform((unsigned int)input.length);
[output appendFormat:#"%C", [input characterAtIndex:index]];
[input replaceCharactersInRange:NSMakeRange(index, 1) withString:#""];
}
return output;
}
-(NSString *)randomStringWithLength: (int) len
{
NSString *letters = #"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
for (int i=0; i<len; i++)
{
[randomString appendFormat: #"%C", [letters characterAtIndex: arc4random() % [letters length]]];
}
return randomString;
}`
I am working in iOS, and I am facing one problem. I have 6 letters, and I want to find all strings that can be generated by these 6 letters. The sequence and length of the string doesn't matter here, also the meaning of the string doesn't matter because we can check the string with a dictionary to find if it is a valid word or not.
So is there any algorithm to find this?
For example:
Input 6 letters are : N T L P A E
Expected output will be:
plan
net
planet
lan
tea
lap
..
..
Here the words in the output are only the valid words, but the output should contain all possible words, even invalid words.
This should probably solve this:
+ (void) logPermutations:(NSArray*)objects
{
if (objects == nil || [objects count] == 0) {
return;
}
NSMutableArray* copy = [objects mutableCopy];
[self logPermutations:copy logedSoFar:#""];
}
+ (void) logPermutations:(NSMutableArray*)objects
logedSoFar:(NSString*)log
{
if (objects == nil || [objects count] == 0) {
return;
}
NSUInteger count = [objects count];
for (NSUInteger i = 0; i < count; ++i) {
id removed = [objects objectAtIndex:i];
[objects removeObjectAtIndex:i];
NSString* newlog = [NSString stringWithFormat:#"%#%#",log,[removed description]];
NSLog(#"%#",newlog);
[self logPermutations:objects logedSoFar:newlog];
[objects insertObject:removed atIndex:i];
}
}
-(NSMutableArray*)genValidWords:(NSString*)tiles
{
/*===============EMPTY PREVIOUS ARRAY=================*/
FINAL_VALID_WORDS=[[NSMutableArray alloc] init];
posWords=[[NSMutableArray alloc] init];
genWords=[[NSArray alloc] init];
/*============EMPTY PREVIOUS ARRAY FINISH=============*/
/*===============POSSIABLE NO OF WORDS FROM GIVEN LETTERS=================*/
int length=[tiles length];
int total=0;
for(long i=2;i<[tiles length]+1;i++)
total=total+([self factorialX:length]/[self factorialX:length-i]);
NSLog(#"POSSIABLE NO OF WORDS FROM LETTERS \"%#\" IS %d",tiles,total);
/*===============POSSIABLE NO OF WORDS FROM GIVEN LETTERS FINISH=================*/
/*===============GET ARRAY OF ALL POSSIABLE WORDS FROM GIVEN LETTERS=============*/
for(int i=2;i<[tiles length]+1;i++)
{
genWords=getPermutations(tiles, i);
[posWords addObjectsFromArray:genWords];
}
NSLog(#"%#",posWords);
return posWords;
}
static NSMutableArray *results;
void doPermute(NSMutableArray *input, NSMutableArray *output, NSMutableArray *used, int size, int level)
{
if (size == level)
{
NSString *word = [output componentsJoinedByString:#""];
//if(check(word))
[results addObject:word];
return;
}
level++;
for (int i = 0; i < input.count; i++)
{
if ([used[i] boolValue])
{
continue;
}
used[i] = [NSNumber numberWithBool:YES];
[output addObject:input[i]];
doPermute(input, output, used, size, level);
used[i] = [NSNumber numberWithBool:NO];
[output removeLastObject];
}
}
NSArray *getPermutations(NSString *input, int size)
{
results = [[NSMutableArray alloc] init];
NSMutableArray *chars = [[NSMutableArray alloc] init];
for (int i = 0; i < [input length]; i++)
{
NSString *ichar = [NSString stringWithFormat:#"%c", [input characterAtIndex:i]];
[chars addObject:ichar];
}
NSMutableArray *output = [[NSMutableArray alloc] init];
NSMutableArray *used = [[NSMutableArray alloc] init];
for (int i = 0; i < chars.count; i++)
{
[used addObject:[NSNumber numberWithBool:NO]];
}
doPermute(chars, output, used, size, 0);
return results;
}
-(double) factorialX: (int) num {
double tempResult = 1;
for (int i=2; i<=num; i++) {
tempResult *= i;
}
return tempResult;
}
/*=======================Call Like this====================
NSmutableArray=getPermutations("Pass your string", pass your lenght);
NsmutableArray=getPermutations("Janak", 2);
NSMutableArray=[self genValidWords:#"Hello"];
will return all possiable combination of two letters
=======================xxxxxxxxxx====================*/
I got the solution,
I want something like following.
-(NSArray*)totalWords:(NSArray*)letters
{
NSMutableArray *output=[[NSMutableArray alloc]init];
for (int i=0; i<letters.count; i++)
{
for (int j=0; j<letters.count; j++)
{
if (i==j) continue;
for (int k=0; k<letters.count; k++)
{
NSString *str=[NSString stringWithFormat:#"%#%#%#",letters[i],letters[j],letters[k]];
if(i!=j && j!=k && i!=k &&[self checkmeaning:str])
[output addObject:str];
for (int l=0; l<letters.count; l++)
{
NSString *str=[NSString stringWithFormat:#"%#%#%#%#",letters[i],letters[j],letters[k],letters[l]];
if(i!=j && j!=k && i!=k && l!=i && l!=j && l!=k &&[self checkmeaning:str])
[output addObject:str];
for (int m=0; m<letters.count; m++)
{
NSString *str=[NSString stringWithFormat:#"%#%#%#%#%#",letters[i],letters[j],letters[k],letters[l],letters[m]];
if(i!=j && j!=k && i!=k && l!=i && l!=j && l!=k && m!=i && m!=j && m!=k && m!=l &&[self checkmeaning:str])
[output addObject:str];
for (int n=0; n<letters.count; n++)
{
NSString *str=[NSString stringWithFormat:#"%#%#%#%#%#%#",letters[i],letters[j],letters[k],letters[l],letters[m],letters[n]];
if(i!=j && j!=k && i!=k && l!=i && l!=j && l!=k && m!=i && m!=j && m!=k && n!=i && n!=j && n!=k &&n!=m &&n!=l && m!=l&&[self checkmeaning:str])
[output addObject:str];
}
}
}
}
}
}
NSLog(#"count :%i",[output count]);
NSLog(#"output array :\n%#",output);
return output;
}