Strings are covered on a high level in this video. Discover the different options of c-string and std-string. An array of chars are compared and discussed with a possible interview question and how to approach it using strings.
- [Instructor] When we discuss the technical portion of the interview, try to imagine how you would approach these topics both verbally and technically. You'll have to explain an answer or solve the problem on white board or computer, and submit for a future review. Because of it's use, the String Data type is a common topic for questions in a C++ interview. We have already discussed the differences and similarities between C and C++, and the String is a data type that they share and yet don't share.
C++ can use the C String Data type by including the C Sting Library. When you create a C String, you are a essentially creating a Char Array. This is how C creates it's Strings. To compare C Strings, you must use the String Compare function. And to copy one String to another, you would use the String Copy function. The C Library, C String, should be included when you are using these Strings. All memory allocation and creation is done in this library.
The C String has the added benefit of requiring the null character at the end of the String. This means that you can compare a String to the null character to test for empty String. This could be useful if there is a required information that the user has left blank. But there is one other String available in C++. The Standard String. The String might be more common, yet neither the C String or the Standard String are default in C++. The String Data type is included in the Standard Library, and can be used like any other data type.
Which means you can do things like compare equality using the double equals sign. As a challenge, if you are given this question to determine if a given String is a palindrome, how would you solve it? Remember, a palindrome is a word that is the same when read forwards and backwards. Taco cat is one of my favorite palindromes. So, we should be able to determine if this is in fact a palindrome. As you see, I declared a Sting and gave it a value.
Now, I'll declare a flag and initialize it to True. Next, I will use a for loop to cycle through the String. I don't need to go through the entire String, just half. If the first half matches the second half, it's a match and a true palindrome. This will save time. So, taco cat is a palindrome. Yay, we knew that, but to test for validity just to make sure our program isn't broken, let's add an extra A and if I add an extra A to Caat, it is not a palindrome.
Which is correct. So, there you have it, this quick scan through. Looping through half of my Array. Only half because I'm just matching the front half to the back. If it doesn't match, as soon as it doesn't match, I'm exiting out of my loop, and then I'm printing, it either is or it isn't a palindrome.
- Getting ready for interviews
- Types of questions you can expect
- Comparing C and C++
- Strings and arrays
- Linked lists
- Stacks and queues