Jump to content

Talk:Comparison of programming languages (string functions)

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by SineBot (talk | contribs) at 05:18, 22 June 2009 (Signing comment by 203.206.162.148 - "ASC: new section"). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

C function toupper() in UpperCase

This is misleading in the article. C doesn't have a function to uppercase a whole string. toupper() takes and returns an integer as its arguments, NOT strings. It's prototype:

int toupper(int c);

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()

void UpperCaseAString(char *theString)//string is a pointer to the first char of the string you want to uppercase.

{

char *myCharPtr = theString;//myCharPtr is a pointer to char - innitialize it to theString

while(*myCharPtr != '\0')//C uses-null terminated strings. *is what's pointed to by myCharPtr
    {
    *myCharPtr = toupper(*myCharPtr);
    myCharPtr ++; //myCharPtr is a pointer to type char so it will be incremented by sizeof(char).
    }
}

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.Senor Cuete (talk) 03:41, 10 May 2008 (UTC)Senor Cuete[reply]

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.Senor Cuete (talk) 03:44, 10 May 2008 (UTC)Senor Cuete[reply]

The <source lang="...">...</source> tag should fix it. Ghettoblaster (talk) 12:43, 10 May 2008 (UTC)[reply]

Compare (integer result, fast/non-human ordering)

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?

#include <string.h>

int strncmp(const char *s1, const char *s2, size_t n);

Senor Cuete (talk) 00:52, 16 May 2008 (UTC)Senor Cuete[reply]

substring

Shouldn't the table row for C just mention the C function strncpy?

#include <string.h>

char *strncpy(char *s1, const char *s2, size_t n);

Why concatenate when you can copy?Senor Cuete (talk) 00:53, 16 May 2008 (UTC)Senor Cuete[reply]

Because strncpy() will not copy a null-terminator if the string is n or more characters long. --Spoon! (talk) 12:13, 16 May 2008 (UTC)[reply]

strings vs lists

"In both Prolog and Erlang, a string is represented as a list (of character codes), therefore all list-manipulation procedures are applicable, though the latter also implements a set of such procedures that are string-specific."

I think this is the same for Haskell, should it also be noted? —Preceding unsigned comment added by 124.171.21.141 (talk) 00:20, 28 June 2008 (UTC)[reply]

Additional procedure/operators

Some further string manipulations for consideration:

  • substring append & prepends: eg in python: s+="ABD"
  • replace substring:
    1. by substring text: eg AWK gsub("Earthling","Martian",string)
    2. by slice: s[3:4]="XY"
  • insert substring at offset.

NevilleDNZ (talk) 08:17, 15 May 2009 (UTC)[reply]

ASC

Came here looking for a Python equivalent to the ASC() function, which, in BASIC/VB6, returns the numeric value of the first character of a string.

Not exactly equivalent to any string function in any language which handles strings differently, but in BASIC it was a string function. —Preceding unsigned comment added by 203.206.162.148 (talk) 05:17, 22 June 2009 (UTC)[reply]