• Register
2 votes


Hello all,

I am a student of C++ programming. I am wondering if there's an abbreviation or a more elegant way of getting the last character of a string. Let’s assume, the given string is “I am a String” then the output should be “I am a Strin

I’ve almost 30 minutes for the problem and couldn’t figure out any legit solution. I hope you people would be helpful.


12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote


You can simply use the strlen() function to find out the position of the char in the string and print it. Just prompt a string from the user and give it a count until its last character’s.

#include <stdio.h>
#include <string.h>
int main(void){
    char str[50];
    printf("Enter a string within FIFTY chars: ");
    printf("\nLast char: %c\n",str[(strlen(str)-1)]);
    return 0;

As you can see, I’ve used str[(strlen(str)-1)] in my print line because the array index starts from zero.

However, I don’t understand the term “Elegant” properly. And I am suggesting another way to solve the problem. Find the elegant way between these two, please.

The second way is about using the Null byte and replace the last character of a String.

#include <string.h>

int main(){
    char string[]={"I am a String"};
    int len;
    char* pos; //position of last char
    len=strlen(string); // saving length of string
    pos=string+len-1; // making pos point to last char of string
    *pos='\0'; // replaced last char with null byte
    return 0;

The output should be:

I am a Strin

I hope this might help you. Leave a kudos if the problem is solved for you.


13 9 6
94,240 points

Related questions

0 votes
1 answer 40 views
Problem: I want to check the last character of string entered by a user. Is there any way of getting the last character of a string and using it to compare values?
asked Oct 17, 2020 Code Learner 5.8k points
1 vote
1 answer 20 views
Problem: Hello kodlogs, I am a new member of this forum. I find this forum very helpful with a bunch of awesome people. Well, I have started learning python for a few days back. I am taking help from a YouTube channel and solving basic problems from a website ... written the third line. However, the program is not printing the expected value yet. Could you please shed some light on this? Thanks.
asked Jun 1, 2020 adamSw 11.3k points
0 votes
1 answer 12 views
12 views asked Oct 27, 2020 xgtx 320 points
0 votes
2 answers 30 views
30 views asked Oct 17, 2020 SapperIt 1.2k points
0 votes
1 answer 11 views
Problem: Hello Developers! I am new to the world of programming and I started learning Java at an academy. My teacher gave us an assingment in which we have to find the index of last occurrence of a character in a string. The program should return the ... those solutions. If someone has better solution then kindly explain or else please explain what substrings are and how I can use it. Thanks
asked Nov 29, 2020 Code Learner 5.8k points
2 votes
1 answer 23 views
Problem: Hello all, I am a new student in the Python programming language. I am learning through a textbook. I just completed the String chapter and started solving the problems of this chapter. Some of the problems are easy to solve for me. I stuck on a point ... understand. I&rsquo;ve seen some of the solutions answered by the good people. If anybody could help me would be great for me. Thanks
asked Mar 29, 2020 Gavin 15.3k points
0 votes
1 answer 12 views
Problem: How to Add character to string in c++
asked 6 days ago kaash_kashi 1.2k points
0 votes
1 answer 4 views
Problem: How to remove character from string in c++
asked 6 days ago kaash_kashi 1.2k points
0 votes
1 answer 16 views
Problem: What is the effective way to replace all occurrences of a character with another character in std::string? Replace portion of string Replaces the portion of the string that begins at character pos and spans len characters (or the part of the string in the range between [i1,i2))
asked Dec 26, 2020 Mashhoodch 9.1k points
0 votes
1 answer 46 views
Problem: I want to learn how can we get the specific numbers of characters from a string. For example get last 4 characters of a string php.
asked Dec 9, 2020 prog_learner 2.5k points