1

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

4 Answers 4

4

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]);
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, so is the NULL character present in all strings even in command line arguments like argv[]. Does strlen() count the \0 character or do you have to put +1 at end?
Yes, the null terminator is present in all strings. Without it you wouldn't know how long a string is because there is no length field. strlen does not count the '\0', only the "real" characters.
1

I think you want this:

char *s = "peter";
char lastChar = s[strlen(s) - 1];
printf("last character in s is %c\n", lastChar);

Comments

1

Your problem is that strings in C are null-terminated. That means that they end in the '\0' character. You have to save space for that. Moreover, arrays are indexed from 0, so name[5] specifies a string with 4 characters and a null character, with the null character at index 4 of name.

You need to fix these:

char name[6]="peter";

so now:

name[0] = 'p'
name[1] = 'e'
...
name[4] = 'r'
name[5] = 0

So your if statement should use index 4, not 5:

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

Finally, to print a single char, use th %c modifier, not %s. Your final printf should read:

printf("last character of name is %c",name[4])

Otherwise you're printing a string that begins from the offset you specified, which (if you allocated enough space in your string) will be '\0', which is an empty string.

Comments

0

There are two big issues here:

  • First is the length of the string
  • Second is the index of the string.

Length

In C, strings are NULL-terminated, meaning they have an invisible \0 at the end to mark the end of the string. You have to account for this invisible charater, so while peter is only 5 letters, you need one more space for the Null-terminator.

char name[6]="peter";  /* implied \0 at the end of the string */

Index

In C, strings and arrays are 0-indexed, meaning that the first element is [0]. In the case of your string, the indexes are:

[0] `p`
[1] `e`
[2] `t`
[3] `e`
[4] `r`
[5] `\0`

So where you expect r to be the 5th character, it is at index 4.

If you declare the string properly (with 6 elements instead of 5), and look at index [4], you'll find that is is r as expected.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.