Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3691620
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:10:06+00:00 2026-05-19T04:10:06+00:00

Just stuck on c syntax regarding strings. Say I have a string like (

  • 0

Just stuck on c syntax regarding strings.

Say I have a string like (name[5]="peter";) in c say if I just wanted to print the last character of string or check the last character of the string, which in this case would be 'r' how can I do this?

The way I was thinking does not seem to work

name[5]="peter";
if(name[5]=="r") printf("last character of name is r");

Question: is there some sort of function to do this that can check one character of array, is a certain value, like name[5] is ‘r’ in string peter or likewise name[1] is ‘n’

Also how do I use printf to print that certain char, having problems using

printf("last character of name is %s",name[5]) ???

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:10:07+00:00Added an answer on May 19, 2026 at 4:10 am

    First thing, strings are null-terminated. For a five-character string you need to allocate a 6-character array to handle the '\0' character at the end of the string.

    char name[6] = "peter";
    // peter is {'p', 'e', 't', 'e', 'r', '\0'}
    

    To check what individual characters are, index the string using square brackets. The first character is index 0, the second is index 1, etc. Also, C makes a distinction between strings and individual characters. A string is written with "double quotes". Characters are written with single quotes: 'r'.

    if (name[4] == 'r') {
        printf("fifth character of name is r\n");
    }
    

    To find the last character you need to know the length of the string. If you know the length ahead of time you can hard code it; otherwise, use the strlen function to calculate the string length. And then subtract 1 because indexes are 0-based.

    if (name[strlen(name) - 1] == 'r') {
        printf("last  character of name is r\n");
    }
    

    To print individual characters with printf you can use the %c format specifier. %c prints a single character.

    printf("fifth character of name is %c\n", name[4]);
    printf("last  character of name is %c\n", name[strlen(name) - 1]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am stuck and I guess it is a syntax thing I just don't
Where I'm stuck is the right parameter syntax to instantiate an object using PHP's
I've wrapped most of wininet with no problems, but now I'm stuck. I am
I have been reading Wikipedia's article on K programming language and this is what
If I have a form of about 40 questions, how do I apply the
Is there a syntax for documenting functions which take a single configuration array, rather
I'm having an issue with the syntax for modules. Basically I'm trying to split
I can't seem to execute SQL that creates a database using a DbCommand object.
Hey, in a tutorial C++ code, I found this particular piece of confusion: PlasmaTutorial1::PlasmaTutorial1(QObject
I am working in a Vim script. One of my functions needs a temporal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.