0

My question is related to "longest common prefix" problem in LeetCode.(https://leetcode.com/problems/longest-common-prefix/)

Here is my code:

#include <string>

class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
    int cnt = 0;
    string result = "";
    char ch = ' '; 
    char standard = ' ';
    
    for(int i=0; i<strs.size(); i++){
        if(strs[i].size() == 0) return result;
    }
    
    if(strs.size() == 1){
       return strs[0];
    } 

    while(1){
        standard = strs[0][cnt];
        for(int i=0; i<strs.size(); i++){
            ch = strs[i][cnt];
            if(ch == '\0' || ch != standard) return result; 
          
        } 
        
        result+=ch;
        cnt++; 
    }
}

};

While iterating every single character in vector's string elements, I tried to handle exceptions like all elements in string vectors are totally equal by using code

if(ch == '\0'...)

But I've learned that string class in C++ does not contain null character at the end of the string, so I thought this code wouldn't work as code that I wrote was meaningless.

But why does this code work...?

Does C++ put null character automatically in string vector's elements while not in string?

Matt Timmermans
  • 45,884
  • 3
  • 35
  • 76
  • Beginning with C++11, reading "one past" the last character of the string produces the null character. Prior to that, the behaviour was undefined. – molbdnilo Aug 30 '21 at 14:07
  • 1
    Relevant: [Does std::string contain null terminator?](https://stackoverflow.com/q/11752705/580083) – Daniel Langr Aug 30 '21 at 14:09
  • @molbdnilo Then do you mean by just accessing the last character of the string appends null character at the end of the string automatically? – legionary7931 Aug 30 '21 at 14:10
  • No, I mean only that `s[s.size()]` returns (a reference to) the null character. (And, judging by [this](https://en.cppreference.com/w/cpp/string/basic_string/operator_at), I was wrong; reading that character has always been well-defined, apparently.) – molbdnilo Aug 30 '21 at 14:16

1 Answers1

0

Yes. A '\0' char is null terminated character. That represents the end of a string. A string under the hood is an array of characters. And memory must know when that array ends so it puts a \0 after the string finishes and that represents the end.

If you try to do something like:

std::str = "Hello"
std::cout << str[str.cout()];

you will not get an output but you will also not get a garbage value. The terminal is actually printing the '\0' character

You can also check this with this program:

std::string tmp = "Hello";

if (tmp[tmp.size()] == '\0')
    std::cout << "True\n";

Since it stasrts to count from 0, the 5th character in the string is actually a null terminated value.

Long story short: Every string has a '\0' at the end of it without actually showing it when you print it nor showing it when you get the size of a string with str.size();

Hope this helped!

Oogway101
  • 53
  • 1
  • 5