Download presentation
Presentation is loading. Please wait.
Published byGervais Phillips Modified over 9 years ago
1
String operations
2
About strings To handle strings more easily, we need to include a library> #include To see what the library allows us to do, look here: http://www.cplusplus.com/reference/cstring/ http://www.cplusplus.com/reference/cstring/ Reminder: a string is nothing more than just an array of characters! The string has a special terminating character ‘\0’ Every character is just a number, look for the value in the ASCII table 20152
3
Strlen() Find the length of a string strlen(myString) myString – the string to find the length of. NULL isn’t counted Returns the length of the string as an integer >>> Write a code snippet where you ask for the user for a sentence and the program outputs its length. Gets() function may help. 20153
4
Strstr() Finds if one string contains another string (finds a substring) strstr(str1, str2) str1 – string, that will be looked into str2 – the substring that will be searched for (a.k.a. the key) A NULL is returned, if the substring is not found a nonzero value (address) is returned, if the substring is found >>> Improve your program so that the user can search for a word in the previously entered sentence. Found / not found is sufficient. 20154
5
Strcmp() Compares 2 strings. The comparison is done one character at a time from the beginning. The characters are compared until the end is reached or a discrepancy appears. strcmp(str1, str2) Str1 ja str2 on are the strings to be compared A NULL is returned, if the strings are equal A negative number is returned, if str2 character is of a smaller value than str1 char A positive number is returned, if str2 character is of a larger value than str2 char >>> Change your existing code so that it couldn’t be run before the user has entered a suitable password. 20155
6
Strcat() Concatenates a copy of the source to the destination string. The original string is preserved, concatenation starts from the old string’s NULL character. destString’s length must be large enough to hold the concatenated string. strcat(destString, sourceString) destString – the target string sourceString – the string that will be concatenated onto another tagastus – the location of the destString. 20156
7
Strcpy() Copies a string to a new location, original is preserved. The string that’s currently at the destString will be overwritten. destString’s length (size) must be sufficient to store the copied string. Strcpy(destString, sourceString) destString – the location of the string where the copy will be stored sourceString – string that will be copied return – the location of the destString 20157
8
strcpy() ja strcat() Add a code snippet that will ask the user for the first and last name The result will be stored in a third string with the following format “Lastnameinitial. Firstname" e.g: "Jane", "Smith" -> "S. Jane" >>> Got all this in one file? This was lab task #1 20158
9
Lab task #2 Create a program, that will randomly generate a n*n square matrix filled with characters. Recall the rand() function. Show the original matrix Show the matrix so that all of the vowels are replaced with the “-” symbol. Show the matrix so that all of the consonants are replaced with the “*” symbol So in total, you need to display 3 different matrices! Hint: ASCII (American Standard Code for Information Interchange) 20159
10
Advanced Recall crossword puzzles where you had to find given words in a matrix of characters Create functionality that would allow Your matrix to be searched for words. The words can be in horizontally, diagonally or vertically aligned. They can also be written backwards. Create the matrix manually for easier testing. 201510
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.