• 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,260 points

Related questions

0 votes
1 answer 107 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 9.9k points
1 vote
1 answer 32 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 14 views
14 views asked Oct 27, 2020 xgtx 320 points
0 votes
1 answer 11 views
Problem How can i read last character of the string in c++
asked Mar 26 Subhendu 4.5k points
0 votes
1 answer 7 views
Problem I am a new programmer in c++, i want to remove last character from a text file in c++, Any one can help me how can i write a source code
asked Apr 13 Subhendu 4.5k points
0 votes
1 answer 6 views
Problem: Can someone propose more elegant way? I cannot find a way out to my above query?
asked Apr 12 Sana8989 8.9k points
0 votes
0 answers 6 views
Problem: I'm having problems during my learning as I mentioned in my question above, I try to find on web search but I can't get proper information, can you help me to continue my project?
asked Mar 29 tuhin1 48.9k points
0 votes
1 answer 21 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 9.9k points
2 votes
1 answer 36 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 1 view
1 view
Problem: Please let me know any solution regarding this .. Delete last character in string python
asked 3 days ago Ifra 37.2k points