Do swift chars relate to clang chars? - objective-c

Swift's String.Index is defined in the docs as
A position of a character or code unit in a string.
The endIndex is
A string’s “past the end” position—that is, the position one greater
than the last valid subscript argument.
and startIndex is
The position of the first character in a nonempty string.
Is it correct to think of Swift chars in the context of C lang chars?
My understanding in Clang is that the index of a character in a string returns the memory space of the character and the string ends on a null character.
So for Swift, the endIndex is the null character and the reason we use String.Index instead of using subscript String with an Int (something like string[0] like in say JavaScript) is because we are handling the memory space of the character.
If this is the right thinking is this because Swift runs on top of Objective-C runtime?

Related

It is necessary to replace all characters of the string with the adjacent character (shift all characters to the right by 1)

It is necessary to replace each character in the string cyclically with the character adjacent to the right, and then collect it into a string again.
Instead of shifting characters to the right, it turns out to increase alphabetically
fun main {
val message = "abcd1234"
val messageSecond = message.map {char -> char + 1}.joinToString ("")
}
Your code is wrong as it increases the ASCII code of each character and as you noted yourself it increases "alphabetically" (not exactly, just in ASCII order)
I can't write the code for you as it seems to be an assignment, but I can give you a hint.
You can actually solve this just by "moving" only one character.
 
Best of luck!

Understanding the following piece of code in smalltalk

I am trying to understand the following piece of code in smalltalk
Character extend [
isGraph [
^ (Character space < self) & (self <= $~)
]
visible [
self isGraph ifTrue: [^ '$', self asString]
ifFalse: [^ self asInteger printStringRadix: 16]
]
]
So basically what that code is doing is to extend the functionality of character by adding two new functions to it. IsGraph returns a boolean value, but I don't understand its purpose. How do you interpret (Character space < self) & (self <= $~)?. Somehow the message space is sent to character and that returns something which is compared to self and then self is compared to $~. Can also someone explain the meaning of the symbol ',' in the iftrue block?
welcome to StackOverflow.
First of all the code is adding two new methods and not functions as this is object-oriented programming.
When you send the space message to the Character class it will return you an instance if that class which represents the space character. isGraph probably means "is graphical" because the characters that precede space in the ASCII table do not have a graphical representation (they are NULL, CR, ESC, etc.) as well as the DEL character that follows ~. Thus with isGraph, you check whether a character is between space and ~ on ASCII table.
visible returns a visible representation of a character and relies on isGraph to decide whether to return the actual character or its integer ASCII representation. The actual character is returned in the Smalltalk's character literal format e.g. $a is used for character a, $3 is used for character 3. Strings are concatenated with the , message.
Actually, one of the main points of Smalltalk is understandability. Thus you should be always able to debug a small piece of code or look at the implementations of a message (like , in your case). But I suspect that you use something like GNU Smalltalk that lacks many of these features.

What does \0 stand for? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does the \0 symbol mean in a C string?
I am new at iPhone Development. I want to know, what does '\0' means in C, and what is the equivalent for that in objective c.
The null character '\0' (also null terminator), abbreviated NUL, is a control character with the value zero. Its the same in C and objective C
The character has much more significance in C and it serves as a reserved character used to signify the end of a string,often called a null-terminated string
The length of a C string (an array containing the characters and terminated with a '\0' character) is found by searching for the (first) NUL byte.
In C, \0 denotes a character with value zero. The following are identical:
char a = 0;
char b = '\0';
The utility of this escape sequence is greater inside string literals, which are arrays of characters:
char arr[] = "abc\0def\0ghi\0";
(Note that this array has two zero characters at the end, since string literals include a hidden, implicit terminal zero.)
The '\0' inside character literals and string literals stands for the character with the code zero. The meaning in C and in Objective C is identical.
To illustrate, you can use \0 in an array initializer to construct an array equivalent to a null-terminated string:
char str1[] = "Hello";
char str2[] = {'H', 'e', 'l', 'l', 'o', '\0'};
In general, you can use \ooo to represent an ASCII character in octal notation, where os stand for up to three octal digits.
To the C language, '\0' means exactly the same thing as the integer constant 0 (same value zero, same type int).
To someone reading the code, writing '\0' suggests that you're planning to use this particular zero as a character.
\0 is zero character. In C it is mostly used to indicate the termination of a character string. Of course it is a regular character and may be used as such but this is rarely the case.
The simpler versions of the built-in string manipulation functions in C require that your string is null-terminated(or ends with \0).
In C \0 is a character literal constant store into an int data type that represent the character with value of 0.
Since Objective-C is a strict superset of C this constant is retained.
It means '\0' is a NULL character in C, don't know about Objective-C but its probably the same.

using characterAtIndex on a decimal point in objective-C

I'm trying to do some stuff with characterAtIndex and I'm stumped. if ([myString characterAtIndex:0]==0) works fine, if I'm looking for the number zero--but if I'm looking for a decimal point, if ([myString characterAtIndex:0]==.) just gives me an error. Is there another way to do this?
Check again. [myString characterAtIndex:0] == 0 will compile, but it won't do what you expect. That condition tests if the first character of your string is the character with the ASCII value of 0, which isn't the numeral 0: it's the NUL character.
-characterAtIndex: returns a value that you can compare with a character literal, which is a character enclosed in single quotes: '0' or '.', for example.
Perhaps something got lost in translation, but looking at your current question, I think this is what the solution would look like (compiles and executes as expected for me):
if([myString characterAtIndex:0] == '.') {
// ...
}
Note that you must use single-quotes (apostrophe), as these are c-style char items (technically ints) and not c-style strings (which would use double-quotes, and technically be an array/pointer)
characterAtIndex returns a unichar character, so use '.' instead.
ie: if ([myString characterAtIndex:0]=='.')

Exclamation(!) operator used on a number in vb.net, what does this do?

I'm looking at inherited code and I found this in a vb.net windows form:
New System.Drawing.SizeF(6.0!, 13.0!)
My question is, what is the significance of the ! (exclamation) operator here? Most of my searching for the exclamation operator ends up returning recordset format or the ! gets ignored in the search and I get hundreds of unrelated items.
It's to force the literal to be a Single.
Visual Basic supports Type Characters:
In addition to specifying a data type in a declaration statement, you can force the data type of some programming elements with a type character. The type character must immediately follow the element, with no intervening characters of any kind.
and:
Literals can also use the identifier type characters (%, &, #, !, #, $), as can variables, constants, and expressions. However, the literal type characters (S, I, L, D, F, R, C) can be used only with literals.
In this case, the ! stands for Single:
Type Characters. Appending the literal type character F to a literal forces it to the Single data type. Appending the identifier type character ! to any identifier forces it to Single.
(emphasis mine)
It is a type character. It means that 6.0 is a Single data type.
http://msdn.microsoft.com/en-us/library/s9cz43ek.aspx show the type characters.