Documente online.
Zona de administrare documente. Fisierele tale
Am uitat parola x Creaza cont nou
 HomeExploreaza
upload
Upload




Traditional string representation in C

visual c en


Traditional string representation in C

In C character strings are represented by a sequence of bytes finished by a trailing zero byte. For example, if you got:

char *Name = "lcc-win32";

You will have in memory something like this:

l



c

c

-

w

i

n

3

2

0

108

99

99

45

119

105

110

51

50

0

We will have at each of the position of the string array a byte containing a number: the ASCII equivalent of a letter. The array will be followed by a zero byte. Zero is not an ASCII character, and can't appear in character strings, so it means that the string finishes there.

This design is quite ancient, and dates to the beginning of the C language. It has several flaws, as you can imme 727j94h diately see:

  • There is no way to know the length of a string besides parsing the whole character array until a zero byte is found.
  • Any error where you forget to assign the last terminated byte, or this byte gets overwritten will have catastrophic consequences.
  • There is no way to enforce indexing checks.

The most frequently used function of this library are:

  • "strlen" that returns an integer containing the length of the string. Example:

int len = strlen("Some character string");

Note that the length of the string is the number of characters without counting the trailing zero. The physical length of the string includes this zero byte however, and this has been (and will be) the source of an infinite number of bugs!

  • "strcmp" that compares two strings. If the strings are equal it returns zero. If the first is greater (in the lexicographical sense) than the second it returns a value greater than zero. If the first string is less than the second it returns some value less than zero. The order for the strings is based in the ASCII character set.

a == b

strcmp(a,b) == 0

a < b

strcmp(a,b) < 0

a >= b

strcmp(a,b) >= 0

  • "strcpy" copies one string into another. strcpy(dst,src) copies the src string into the dst string. This means it will start copying characters from the beginning of the src location to the dst location until it finds a zero byte in the src string. No checks are ever done, and it is assumed that the dst string contains sufficient space to hold the src string. If not, the whole program will be destroyed. One of the most common errors in C programming is forgetting these facts.
  • "strcat" appends a character string to another. strcat(src,app) will add all the characters of "app" at the end of the "src" string. For instance, if we have the string pointer that has the characters "lcc-win32" as above, and we call the function strcat(str," compiler") we will obtain the following sequence:

l

c

c

w

i

n

c

o

m

p

i

l

e

r

The common operations for strings are defined in the header file <string.h>

Function

Purpose

strcat

Appends strings.

strchr

Find the first occurrence of character in a string

strrchr

Find the last occurrence of a character in a string

strcmp

Compares two strings

strncmp

Compare strings up to a maximum length

strnicmp

Compare strings up to a maximum length ignoring case

strcol

Compare strings using locale-specific information.

strcpy

Copy a string into another

strcspn

Find a substring in a string

strupr

Convert string to upper case

strlwr

Convert string to lower case

strerror

Get a system error message (strerror) or prints a user-supplied error message (_strerror).

strlen

Find the length of a string

strncat

Append characters of a string.

strncpy

Copy strings up to a maximum length

strpbrk

Scan strings for characters in specified character sets.

strspn

Find the first substring

strstr

Find a substring

stristr

Find a string ignoring case.

strtok

Find the next token in a string

strdup

Duplicate a string. Uses malloc.

strrev

Reverse characters in a string

strtrim

Eliminate redundant blanks from a string.

strset

Set characters in a string to a character.

You will find the details in the online documentation.

Besides these functions in the standard C library, the operating system itself provides quite a few other functions that relate to strings. Besides some relicts of the 16 bit past like lstrcat and others, we find really useful functions, especially for UNICODE handling.

CharLower

CharLowerBuff

CharNext

CharNextExA

CharPrev

CharPrevExA

CharToOem

CharToOemBuff

CharUpper

CharUpperBuff

CompareString

FoldString

GetStringTypeA

GetStringTypeEx

GetStringTypeW

IsCharAlpha

IsCharAlphaNumeric

IsCharLower

IsCharUpper

LoadString

lstrcat

lstrcmp

lstrcmpi

lstrcpy

lstrcpyn

lstrlen

MultiByteToWideChar

OemToChar

OemToCharBuff

WideCharToMultiByte

wsprintf

wvsprintf


Document Info


Accesari: 1043
Apreciat: hand-up

Comenteaza documentul:

Nu esti inregistrat
Trebuie sa fii utilizator inregistrat pentru a putea comenta


Creaza cont nou

A fost util?

Daca documentul a fost util si crezi ca merita
sa adaugi un link catre el la tine in site


in pagina web a site-ului tau.




eCoduri.com - coduri postale, contabile, CAEN sau bancare

Politica de confidentialitate | Termenii si conditii de utilizare




Copyright © Contact (SCRIGROUP Int. 2024 )