Talk:Comparison of programming languages (string functions): Difference between revisions
Content deleted Content added
Christian75 (talk | contribs) +{{WikiProject Computing|class=|importance=}} |
→Mentioning strtok as C/C++ way of splitting strings: new section |
||
(7 intermediate revisions by 6 users not shown) | |||
Line 1:
{{WikiProject
{{WikiProject Computing|importance=mid}}
}}
==C function toupper() in UpperCase==
Line 9 ⟶ 10:
If c is a lowercase letter (a-z), topupper() returns the uppercase version (A-Z). Otherwise toupper() returns c unchanged. toupper() does not convert international characters (those with ASCII codes over 0x80), like ă or ç. To uppercase a whole string you need to write a function something like this:
<
#include <ctype.h> //standard C header file with the prototype of toupper()
Line 24 ⟶ 25:
}
}
</syntaxhighlight>
In C strings are essentially pointers to a character and they end where there is a NULL ('\0') character. It would be worthwhile to explain what strings are in different languages.[[User:Senor Cuete|Senor Cuete]] ([[User talk:Senor Cuete|talk]]) 03:41, 10 May 2008 (UTC)Senor Cuete
Line 30 ⟶ 31:
The 1. should appear as a pound sign and the box is put there by Wiki's text engine. I didn't type it like that.[[User:Senor Cuete|Senor Cuete]] ([[User talk:Senor Cuete|talk]]) 03:44, 10 May 2008 (UTC)Senor Cuete
:The <nowiki><
== Compare (integer result, fast/non-human ordering) ==
Line 36 ⟶ 37:
In the table row for C, why would you go through the hassle of writing your own function when you could call the C function strncmp?
<
int strncmp(const char *s1, const char *s2, size_t n);
</
== substring ==
Shouldn't the table row for C just mention the C function strncpy?
<
char *strncpy(char *s1, const char *s2, size_t n);
</syntaxhighlight>
Why concatenate when you can copy?[[User:Senor Cuete|Senor Cuete]] ([[User talk:Senor Cuete|talk]]) 00:53, 16 May 2008 (UTC)Senor Cuete
Line 134 ⟶ 135:
| M || o || n || t || r ||e || ◌́ ||a ||l
|}
== "Code" format ==
The "code" tags on the keywords in the tables (or perhaps other changes) have destroyed the formatting, making the tables almost illegible. If you go back a decade and look the original tables, you'll see that the keywords are clearly delimited, making the tables clear and easy to read.
The present formating makes the whole excercise almost worthless: if you can't read it easily, whats the point of having pages of text? <!-- Template:Unsigned IP --><small class="autosigned">— Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[Special:Contributions/203.206.162.148|203.206.162.148]] ([[User talk:203.206.162.148#top|talk]]) 09:27, 18 July 2017 (UTC)</small> <!--Autosigned by SineBot-->
== How-to guide ==
It seems to me that this article, as useful as it is, is outside of Wikipedia's scope, in light of the principle that [[WP:NOTHOWTO|Wikipedia is not a how-to guide]], which is exactly what this article is. [[User:Largoplazo|Largoplazo]] ([[User talk:Largoplazo|talk]]) 10:00, 18 June 2020 (UTC)
== Mentioning strtok as C/C++ way of splitting strings ==
<code>strtok(char *restrict str, const char *restrict delim)</code>returns tokens (aka split strings). This is essentially what string.split does in most other languages, except it doesn't allocate memory to store array of tokens, instead just mutating original string (replacing delimiter with '\0')[https://stackoverflow.com/questions/3889992/how-does-strtok-split-the-string-into-tokens-in-c] and returning tokens in order of occurence, one by one.
Also it never returns empty tokens[https://en.cppreference.com/w/c/string/byte/strtok].
"Proper" implementation of split (using strtok) is something like:<syntaxhighlight lang=c>#include <stdlib.h>
#include <string.h>
struct stringArray {
size_t size;
char **strings;
};
struct stringArray splitString(char *restrict str, const char *restrict delim) {
char **strings = malloc(sizeof(char *));
if (strings == NULL)
abort();
char *token = strtok(str, delim);
size_t count = 0, allocated = 1;
while (token != NULL) {
if (allocated >= count) {
strings = realloc(strings, (allocated *= 2) * sizeof(char *)); // Doubling reallocation, to provide acceptable performance
if (strings == NULL)
abort();
}
strings[count++] = token;
token = strtok(NULL, delim);
}
if (allocated != count) {
strings = realloc(strings, count * sizeof(char *));
if (strings == NULL)
abort();
}
return (struct stringArray){count, strings};
}
</syntaxhighlight> [[Special:Contributions/5.189.81.197|5.189.81.197]] ([[User talk:5.189.81.197|talk]]) 15:25, 27 July 2024 (UTC)
|