Given a string s. Return the last character of the given string s.
Input : s = "takeuforward"
Output : d
Explanation : The last character of given string is "d".
Input : s = "goodforyou"
Output : u
Explanation : The last character of given string is "u".
Input : s = "lovecoding"
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Function to return the last character of the string
char lastChar(string &s) {
// Return last character of string
return s[s.size() - 1];
}
};
int main() {
// Creating an instance of Solution class
Solution sol;
string s = "example";
// Function call to get the last character of the string
char ans = sol.lastChar(s);
cout << "The last character is: " << ans;
return 0;
}
class Solution {
// Function to return the last character of the string
public char lastChar(String s) {
// Return last character of string
return s.charAt(s.length() - 1);
}
public static void main(String[] args) {
// Creating an instance of Solution class
Solution sol = new Solution();
String s = "example";
// Function call to get the last character of the string
char ans = sol.lastChar(s);
System.out.println("The last character is: " + ans);
}
}
class Solution:
# Function to return the last character of the string
def lastChar(self, s):
# Return last character of string
return s[-1]
# Creating an instance of Solution class
sol = Solution()
s = "example"
# Function call to get the last character of the string
ans = sol.lastChar(s)
print("The last character is:", ans)
class Solution {
// Function to return the last character of the string
lastChar(s) {
// Return last character of string
return s[s.length - 1];
}
}
// Creating an instance of Solution class
const sol = new Solution();
const s = "example";
// Function call to get the last character of the string
const ans = sol.lastChar(s);
console.log("The last character is:", ans);