1 of 14

Operations on Strings

ESC101: Fundamentals of Computing

2 of 14

Some common operations on strings

2

  • Compute the length of a string.
  • Copy one string into another string variable
  • Concatenate one string with another.
  • Search for a substring in a given string.
  • Reverse a string
  • Find first/last/k-th occurrence of a character in a string
    • … and more
  • Case sensitive/insensitive versions

ESC101: Fundamentals of Computing

3 of 14

Computing the length of a string

3

char str[10] = “Hello”;

int i = 0;

while(str[i] != ’\0’)

i++;

printf(“Length of %s is %d”,str,i);

ESC101: Fundamentals of Computing

4 of 14

Read a string and also compute its length

4

int main() {

char str[100];

char ch;

int i = 0;

ch = getchar();

while(1){

if(ch==‘\n’) break;

str[i] = ch;

++i;

ch = getchar();

}

str[i] = ‘\0’;

printf("Length of %s is %d",str,i);

return 0;

}

Read the first character

If found a newline, break

Read the next character

Let’s put ‘\0’ in the end to mark the end of string

Not a newline. Store the read character at index i of str

Will store the length

ESC101: Fundamentals of Computing

5 of 14

Copying a string

5

  • We cannot copy content of one string variable to other using assignment operator

    • This is true for any array variable.
    • Error because array initializer must be a list (comma separated values in {}) or a string.
  • We need to do element-wise copying

char str1[] = "Hello";

char str2[] = str1;

WRONG

Array type is not assignable.

C Pointers needed (will see this later)!

ESC101: Fundamentals of Computing

6 of 14

Copying a string element-by-element

6

  • Goal: Copy contents of string src into string dest.
  • Declare dest with size at least as large as src.
  • Use a loop to copy elements one-by-one
  • Note the use of ‘\0’ for loop termination

char src[100] = “Hello World”;

char dest[100];

int i;

for (i = 0; src[i] != '\0’; i++)

dest[i] = src[i];

dest[i] = '\0';

Part of the loop

Not part of the loop

ESC101: Fundamentals of Computing

7 of 14

string.h

7

  • Many string operations are already implemented in string.h

  • It is a header file (“h” for header) with various functions on Strings

  • strlen(s): returns length of string s (without ‘\0’)

  • strcpy(d, s): copies s into d

  • strcat(d, s): appends s at the end of d (‘\0’ is moved to the end of result)

ESC101: Fundamentals of Computing

8 of 14

string.h

8

  • strcmp(s1, s2): return an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.

  • Example:

  • Prints the value ‘l’-’p’ which is -4.

char str1[] = "Hello", str2[] = "Helpo";

int i = strcmp(str1,str2);

printf("%d", i);

ESC101: Fundamentals of Computing

9 of 14

string.h

9

  • strncpy(d, s, n)
  • strncat(d, s, n)
  • strncmp(d, s, n)
    • restrict the function to “n” characters at most (argument n is an integer)
    • first two functions-- Truncate the string s to the first “n” characters.
    • third function-- Truncate the strings d, s to the first “n” characters.

char str1[] = "Hello", str2[] = "Helpo";

printf("%d",strncmp(str1,str2,3));

0

ESC101: Fundamentals of Computing

10 of 14

string.h

10

  • strcasecmp, strncasecmp:

case insensitive comparison.

  • Example:

  • strcmp gives -32 because ‘E’ < ‘e’ .
    • ‘E’-‘e’ = -32 .
  • strcasecmp finds the first 3 characters the same (ignoring case) gives -4 because ‘l’ - ‘p’ = -4

-32 -4

char str1[] = "HELLO", str2[] = "Helpo";

int i = strcmp(str1,str2);

int j = strcasecmp(str1,str2);

printf("%d %d", i, j);

ESC101: Fundamentals of Computing

11 of 14

string.h

11

  • Many more utility functions.

  • strupr(s) : converts lower to upper case.
  • strlwr(s) : converts upper to lower case.
  • strstr(S,s) : searches string s in string S (example: strstr(“Hello”,”ll”);). Returns a pointer(memory address) to the first occurrence.

  • All functions depend on ‘\0’ as the end-of-string marker.

ESC101: Fundamentals of Computing

12 of 14

Another useful string function

  • atoi(str): converts a string into integer, e.g. atoi(“123”) will return 123 (integer). So we can write int a = atoi(“123”);
  • atoi(str) will keep reading the string str until it finds a non-digit character in str and will return the integer containing the read digit characters in str
  • atoi(“12abc3”) will return 12
  • If no digit symbols found in str, atoi(str) will return 0
  • If the read integer is larger than the range of integers, garbage will be returned
  • Other function atof(str), atol(str) – return float and long
  • Can also convert int/long to string (itoa, ltoa, etc)

12

ESC101: Fundamentals of Computing

13 of 14

Strings: Summary

  • Strings are character arrays
  • The last character is ‘\0’ (null character) and marks end of string
  • Many direct operations (e.g., assignment) not possible for strings. Have to be done element-wise (e.g., using a loop)
  • string.h contains many useful functions (so you don’t need to write functions for basic operations, such as finding the length of string, copying one string into another, etc)

13

14 of 14

14