Talk:Comparison of programming languages (string functions): Difference between revisions

Content deleted Content added
 
(2 intermediate revisions by 2 users not shown)
Line 1:
{{WikiProject Computingbanner shell|class=B|importance=mid}}
{{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:
 
<sourcesyntaxhighlight lang="c">
#include <ctype.h> //standard C header file with the prototype of toupper()
 
Line 24 ⟶ 25:
}
}
</syntaxhighlight>
</source>
 
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><sourcesyntaxhighlight lang="...">...</sourcesyntaxhighlight></nowiki> tag should fix it. [[User:Ghettoblaster|Ghettoblaster]] ([[User talk:Ghettoblaster|talk]]) 12:43, 10 May 2008 (UTC)
 
== 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?
 
<sourcesyntaxhighlight lang="C">#include <string.h>
 
int strncmp(const char *s1, const char *s2, size_t n);
</sourcesyntaxhighlight>[[User:Senor Cuete|Senor Cuete]] ([[User talk:Senor Cuete|talk]]) 00:52, 16 May 2008 (UTC)Senor Cuete
 
== substring ==
 
Shouldn't the table row for C just mention the C function strncpy?
<sourcesyntaxhighlight lang="C">#include <string.h>
 
char *strncpy(char *s1, const char *s2, size_t n);
</syntaxhighlight>
</source>
 
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 144 ⟶ 145:
 
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)