0% found this document useful (0 votes)
48 views

Strings

C strings are sequences of characters terminated by a null character. They are accessed using pointers and functions like strlen, strcpy, strcmp provide operations on strings. Key points: - Strings are arrays of characters with null termination - String literals result in allocated memory for characters plus null - Functions like strlen, strcpy, strcmp provide common string operations - strlen returns length without null, strcpy copies one string to another, strcmp compares strings

Uploaded by

saiarvinth
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Strings

C strings are sequences of characters terminated by a null character. They are accessed using pointers and functions like strlen, strcpy, strcmp provide operations on strings. Key points: - Strings are arrays of characters with null termination - String literals result in allocated memory for characters plus null - Functions like strlen, strcpy, strcmp provide common string operations - strlen returns length without null, strcpy copies one string to another, strcmp compares strings

Uploaded by

saiarvinth
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

C strings

1
Strings
 Sequence of zero or more characters, terminated by NULL
‘/0’ (literally, the integer value 0)
 NULl terminates a string
 important for strlen() – length doesn’t include the NULL
 Strings are accessed through pointers/array names
 string.h contains prototypes of many useful functions

2
String literals
 Evaluating ″dog″ results in memory allocated for three
characters ′d ′, ′ o ′, ′ g ′, plus terminating NULL
char *m = ″dog″;
 Note: If m is an array name, subtle difference:
char m[10] = ″dog″;

10 bytes are allocated for this array


This is not a string literal;
It’s an array initialize in disguise!
Equivalent to {′d′,′o′,′g′,
′\0′}

3
String functions
 C never lets us assign entire arrays, we use the strcpy function to copy one
string to another
 source string write to destination location
 strcpy(dst,src);
 char string1[] = "Hello, world!";
 char string2[20];
 strcpy(string2, string1);
 String2 = Hello, world!“
 C program
strcpy(char dest[], char src[])
{ int i = 0;
while(src[i] != '\0')
{ dest[i] = src[i]; i++; }
dest[i] = '\0'; }
4
strlen() and size_t
size_t strlen(string s1);
/* returns length of string with out the null constant */
 size_t is an unsigned integer type
 int t =strlen(“india”); t=5

C program
strlen(s)
{
int i=0,len=0;
while( s[i]!=‘\0’)
i++;
len++;
}

5
strcmp() “string comparison”
int strcmp(s1,s2); Case discriminative
 strcmp does not return a Boolean, true/false
 Return zero/nonzero answer
 returns a value less than zero if s1 precedes s2 in lexicographical order;
 returns zero if s1 and s2 are equal;
 returns a value greater than zero if s1 follows s2.
 seems reasonable to assume that strcmp returns “true” (nonzero) if s1 and
s2 are equal; “false” (zero) otherwise
 In fact, exactly the opposite is the case!
 int stricmp(s1,s2); Non case senstive
 strcmp(“hello”,”Hello”); output !=0
 stricmp(“hello”,”Hello”); output =0
 strncmp(s1,s2,n) compares only the two string upto specified length n
 Strncmp(“Goodmorning”,”Goodday”,4) output -0
6
string functions strcat()
 strcat() which concatenates strings.
 It append one string onto the end of another.
 NOT a new string third string
 strcat(target,source) source string appened with target
 strcat(“good”,”morning”); output =goodmorning
 strncat(target,source,n)

Copies the source up to specified length n


 strncat(“honey”,”wellcome”,4); output =honeywell

7
strlwr () function strupr () function

 Strlwr() function converts all characters in a string from


uppercase to lowercase.
 strlwr(string);
 strlwr(“INFOSYS”) converts to infosys.
 Strupr() function converts all characters in a string from
lowercase to uppercase
 strup(string);
 strupr(“Hello”) converts to HELLO
strrev() function:

 This function reverses the characters in a string.


 strrev(“india”); output aidni
String searching
char *strpbrk(char const *str, char const *group);
/* return a pointer to the first character in str
that matches *any* character in group;
return NULL if there is no match */

size_t *strspn(char const *str, char const *group);


/* return number of characters at beginning of str
that match *any* character in group */

10 CS 3090: Safety Critical Programming in C


strtok “string tokenizer”
char *strtok(char *s, char const *delim);
/* delim contains all possible ″tokens″:
characters that separate ″tokens″.
if delim non-NULL:
return ptr to beginning of first token in s,
and terminate token with NUL.
if delim is NULL:
use remainder of untokenized string from the
last call to strtok */

11 CS 3090: Safety Critical Programming in C


strtok in action
for ( token = strtok(line, whitespace);
token != NULL;
token = strtok(NULL, whitespace))
printf(″Next token is %s\n″, token);

d o g NUL c a t NUL NUL

NUL

line token

12 CS 3090: Safety Critical Programming in C


An implementation of strtok
char* strtok(char *s, const char *delim) {
static char *old = NULL; old contains the remains
of an earlier s value
char *token; (note use of static)
if (! s) { s = old; if (! s) return NULL; }
if (s) { NULL has been passed in for s,
s += strspn(s, delim); so consult old
if (*s == 0) { old = NULL; return NULL; }
}
strspn returns number of delimiters
token = s; at beginning of s – skip past these characters
s = strpbrk(s, delim);
if (s == NULL) old = NULL;
else { *s = 0; old = s + 1; }
return token; strpbrk gives the position of the next delimiter.
} s is updated to this position, but token still points to
the token to return.
13 CS 3090: Safety Critical Programming in C
Memory operations
 Like string operations, work on sequences of bytes
 but do not terminate when NUL encountered
void *memcpy(void *dst, void const *src, size_t length);
void *memcmp(void const *a, void const *b, size_t length);
 Note: memmove works like memcpy, but allows overlapping source,
destination regions
 Remember, these operations work on bytes
 If you want to copy N items of type T, get the length right:
memcpy(to, from, N * sizeof(T))

14 CS 3090: Safety Critical Programming in C

You might also like