How can I rearrange the chars in a char*? - iphone

I have char* myChar = "HELLO". I would like to switch the places of the E and the O. I tried doing myChar[1] = myChar[4], but that doesn't work. Please help!

First off, that string literal is probably being stored in read-only memory. You can fix that by declaring the string as an array of characters:
char myChar[] = "HELLO";
To swap the characters, you'll have to use a temporary variable:
char c1 = myChar[1];
myChar[1] = myChar[4];
myChar[4] = c1;

You assigned whatever is in myChar[4] into myChar[1]. (that's all you did there)
You need to create a temporary variable char temp; and do the following:
Edit: As mentioned by Tim Cooper, char myChar[] = "HELLO"; - // This will remove it's constness.
temp = myChar[1];
myChar[1] = myChar[4];
myChar[4] = temp;
This is a very common 'algorithm' to swap two things.

Related

Remove end folder part of string in MATLAB

Say if we have this string
a = 'C:/my_folder/folder/mac/data/';
How can I use regexprep to reduce the string to:
'C:/my_folder/folder/mac/';
Actually, I found a way to do it.
[pathstr] = fileparts(a);
regexprep(pathstr, '(?<=/)[^/]*$', '')
You can try this method to cut 5 char at end of string
a = a(1:end-5)

what is the substr? in the systemverilog

Int fd;
String str;
fd = $fopen(path, "r");
Status= $fgets(str, fd);
cm = str.substr(0,1);
cm1= str.substr(0,0);
I want to know what is substr function? What is the purpose above that??
The substr function returns a new string that is a substring formed by characters in position i through j of str. Very similar to examples posted here.
module test;
string str = "Test";
initial
$display(str.substr(0,1));
endmodule
The output will be:
>> Te
As you can see in section 6.16.8, IEEE SystemVerilog Standard 1800-2012.
substr function, as it name suggests, subtracts, or takes a chunk from a bigger string, in systemverilog.
Example:
stri0 = "my_lago";
stri1 = stri0.substr(1,5);
$display("This will give stri1 = %s" , stri1);
....
OUTPUT :- This will give stri1 = y_lag
Substring: This method extracts strings. It needs the Position of the substring ( start index, length). It then returns a new string with the characters in that range.
C# program Substring
using System;
class Program
{
static void Main()
{
string input = "ManCatDog";
// Get Middle three characters.
string subString = input.Substring(3, 6);
Console.WriteLine("SubString: {0}", subString);
}
}
Output
Substring: Cat

How to split Matlab string into two with known suffix?

I need to split a string into two components. As an example I have the string:
s = 'Hello1_1000_10_1_data'
and I want to split it into the two strings
str1 = 'Hello1_1000_10_1'
and
str2 = '_data'
the important point is that I can't be too sure of the format of the first string, the only thing that is sure is that the 'suffix' which is to be read into the second string always reads '_data'. What is the best way to do this? I looked up the documentation on strtok and regexp but they do not seem to offer me what I want.
If you always know the length of the suffix, you could just use that:
s = 'Hello1_1000_10_1_data'
str1 = s(1:end-5)
Or otherwise:
s = 'Hello1_1000_10_1_data'
suffix = length('_data')
str1 = s(1:end-suffix)
You can use:
s = 'Hello1_1000_10_1_data';
str = regexp(s, '(.*)(_data)', 'tokens'){1};
str{1} %// Hello1_1000_10_1
str{2} %// _data
If _data occurs several times in the file name, this will still work.
You can also use strsplit() as follow:
s = 'Hello1_1000_10_1_data';
suffix = '_data';
str = strsplit(s,suffix);
str1 = str{1}
In addition, you can use strsplit() with multiple delimiters.
You can use strfind():
s = 'Hello1_1000_10_1_data';
suffix = '_data';
i = strfind(s,suffix);
if any(i)
i = i(end);
prefix = s(1:i-1);
end

Please explain this specific line in my Java Code?

I have just started learning Java and I reached till arrays , I was preparing this program(From a book) on replacing space ' ' with '.' (dots) and i am not able to understand this specific line (its not mentioned even in the book I am learning from).
Please help me out.
class SpaceRemover{
public static void main(String[] args){
String mostFamous = "Hey there stackoverFLow ";
char [] mf1 = mostFamous.toCharArray();
for(int dex = 0; dex<mf1.length;dex++)
{
char current = mf1[dex]; // What is happening in this line ??
if (current != ' ') {
System.out.print(current);
}
else{
System.out.print('.');
}
}
System.out.println();
}
}
Someone please explain what is happening in "char current = mf1[dex];"
Thanks a lot for your time.
You are getting the dexth character/item within the character array mf1 (hence mf1[dex]) and storing it into the local variable current.
Basically a String in java is an array of characters. So what the above code does is converts the string to an array of chars so that it can access each index of the array later on. Then the code enters into a for loop in order to iterate through all the indecies of the char array.
Assuming that that is already clear to you, the code now creates a char variable which holds the current index of the array.
char current = mf1[dex];
mf1 is your char array that represents the string. dex is the current index of the char that is determined by the for loop. So by doing this we can check each character (letter) of the char array. Now if the char "current" is a blank space we can replace it with a dot.
It's getting the character at index idx in the array mf1 and storing its value in the current variable.
The for-loop is iterating the string mostFamous character by character.
the line you are asking is to get the character at specific position. Function is similar to JavaScript's charAt(i)
char current = mf1[dex];
This line gets values from the mf1 char array and assign to the current variable according to the dex, dex works as index to the array element and it increments with the running loop.
The line
char current = mf1[dex];
is placed inside a for loop where the variable dex is incremented each time the loop is iterated. The variable dex is the zero-based index of the array. On the left hand side of the assignment operator (=), you are declaring a variable named current of type char. On the right hand side of the assignment operator you are accessing the dex-th character of your CharArray, if you start counting from zero. The assignment operator binds the variable you declared with the value of the character you specified on the right hand side.
For example, the first time the loop is run, dex would start at 0, hence mf1[dex] (or mf1[0]) is just 'H'.
Here is solution
class SpaceRemover{
public static void main(String[] args){
String mostFamous = "Hey there stackoverFLow ";
char [] mf1 = mostFamous.toCharArray();
// mf1 contains={'H', 'e','y',' ','t','h',.........}
for(char current: mf1)
{
//the for-each loop assigns the value of mf1 variable to the current variable
//At first time the 'H' is assigned to the current and so-on
System.out.print(current==' '?'.':current );
}
System.out.println();
}
}
}
It assigns the element of the char array mf1 at the index dex to the char variable current.
Note that the for loop and that line may be simplified by using the foreach syntax; these two code blocks are equivalent:
// Your code
for(int dex = 0; dex<mf1.length;dex++) {
char current = mf1[dex];
// Equivalent code
for (char current : mf1) {
But further, the whole method may be replaced by one line:
public static void main(String[] args){
System.out.println("Hey there stackoverFLow ".replace(" ", "."));
}
char current = mf1[dex];
this will return the char element in char array whose index is dex
This is quite a basic usage of array.
Good luck with your study.
After this statement, char [] mf1 = mostFamous.toCharArray();
mf1[0]=H, mf1[1]=e, mf1[1]=y...
so at this line, char current = mf1[dex];
so, in first iteration, current=H, second iteration current=e...

UILabel is not displaying the correct string

I have a UILabel I created on one of my views in my application. I have it set to a static text in IB, but I'm going to change that text programmatically.
Here's how I was trying to do it.
fahrenheitLabel.text = (#"Temperature: %#", text);
//fahrenheitLabel is my UILabel
//"text" is a NSString
I expected the output to be Temperature: 84, for example.
No matter what order I try it, however, I always just get 84.
When I do NSLog(#"Temperature: %#", text); I get the desired result in the Console.
Is there any reason why my UILabel is not displaying the same result as the NSLog call?
Thank you for your help!
Should be:
fahrenheitLabel.text = [NSString stringWithFormat:#"Temperature: %#",text];
NSLog(,); is a function call that is set up for formatting text and will work. (,); has nothing to do with formating text and won't work.
In fact (,) is just the comma operator. These will be evaluated in order and the last one returned. For example:
int a = 1;
int b = 2;
int c = 3;
int x = a, b, c;
Will leave x with the value of c, that is 3.
In your case the comma operator will just return the last item which is "text" and which is why you see 84 all the time.
You should use the NSString formating calls to combine strings the way that you want.