CHARACTER MANIPULATION AND STRING CSI 121 Structured Programming







![STRING DECLARATION • char str[100]; • A character array of length 100 • What STRING DECLARATION • char str[100]; • A character array of length 100 • What](https://slidetodoc.com/presentation_image_h2/a4a7ad3b24f727bd85e772671579e1a5/image-8.jpg)







![THE STRING. H HEADER: STRCPY • Remember char str[] = “SPL LAB”; ? • THE STRING. H HEADER: STRCPY • Remember char str[] = “SPL LAB”; ? •](https://slidetodoc.com/presentation_image_h2/a4a7ad3b24f727bd85e772671579e1a5/image-16.jpg)

- Slides: 17
CHARACTER MANIPULATION AND STRING CSI 121 – Structured Programming Language Course teacher: Minhajul Bashir minhajul@cse. uiu. ac. bd 2/5/2022
Here, we know the length of the string But sometimes, we might not What to do then? minhajul@cse. uiu. ac. bd 2/5/2022 2
STRING INPUT AND OUTPUT So can we safely assume that the string has length 100? NO!!! minhajul@cse. uiu. ac. bd 2/5/2022 3
Input Output So there is no harm in taking a smaller string as input Then how to know end of string? minhajul@cse. uiu. ac. bd 2/5/2022 4
END OF STRING • In C, a special character is used to mark the end of a string • It is called the null character, and represented by ‘ ’ minhajul@cse. uiu. ac. bd 2/5/2022 5
END OF STRING 0 1 2 S P L 3 4 5 6 7 8 9 L A B … … • The above table shows a character array of size 10 • The length of the string stored here is 7 • The end is marked with ‘ ’ minhajul@cse. uiu. ac. bd 2/5/2022 6
END OF STRING 0 1 2 S P L minhajul@cse. uiu. ac. bd 3 4 5 6 7 8 9 L A B … … 2/5/2022 7
STRING DECLARATION • char str[100]; • A character array of length 100 • What is the maximum number of characters in this string? ==99 • char str[] = “Welcome to string!”; • Initializes a string of 18 characters • What is the length of the array? ==19 • Because we need to have an extra space for the null character minhajul@cse. uiu. ac. bd 2/5/2022 8
STRING INPUT AND OUTPUT • How about good-old scanf and printf? • printf works perfectly • printf(“%sn”, str); • scanf … well not so perfectly minhajul@cse. uiu. ac. bd 2/5/2022 9
INPUT STRING USING SCANF Why not &str? Input Output minhajul@cse. uiu. ac. bd 2/5/2022 10
SCANF VS GETS • scanf inputs string until it finds a whitespace in the input stream • gets inputs string until it finds a new line in the input stream • That is why scanf only scanned “SPL”, while gets scanned the whole string “SPL LAB” • If our input string might contain spaces, then gets should be used • scanf is usually not used for string input minhajul@cse. uiu. ac. bd 2/5/2022 11
LET’S DO SOME CODING!!! P. S. We will be back to the slides after coding minhajul@cse. uiu. ac. bd 2/5/2022
THE STRING. H HEADER • Contains many functions for manipulating strings • We will learn a few functions • We will also try to implement these ourselves after a few more classes minhajul@cse. uiu. ac. bd 2/5/2022 13
THE STRING. H HEADER: STRLEN • The function strlen gives us the length of a given string • strlen(“SPL LAB”); will return 7 • strlen(“Tyrion Lannister”); will return 16 • strlen(“Meena Raju o Mithu”); will return 18 minhajul@cse. uiu. ac. bd 2/5/2022 14
THE STRING. H HEADER: STRCMP • The function strcmp compares two strings str 1 and str 2, and returns the following: • 0 if equal • Negative value if str 1 is lexicographically less than str 2 • Positive value if str 1 is lexicographically greater than str 2 • strcmp(“SPL LAB”, “SPL LAB”); will return 0 • strcmp(“SPL LAB”, “SPL LAD”); will return a negative value • strcmp(“SPL LAB”, “SPL LAA”); will return a positive value minhajul@cse. uiu. ac. bd 2/5/2022 15
THE STRING. H HEADER: STRCPY • Remember char str[] = “SPL LAB”; ? • We can initialize string like this only while declaring • We cannot write str = “SPL LAB”; in the middle of the code • We can achieve this using strcpy • strcpy(str, “SPL LAB”); copies “SPL LAB” to str minhajul@cse. uiu. ac. bd 2/5/2022 16
THE STRING. H HEADER: STRCAT • Concatenates a string to a variable • Suppose str[] = “My name is Khan” • strcat(str, “and I am not a terrorist”); adds “and I am not a terrorist” to the end of str • What is the content of str after executing this? • My name is Khanand I am not a terrorist minhajul@cse. uiu. ac. bd 2/5/2022 17