1

I'm quite new to C and as part of my task, I must remove the the newline character that is added at the end of a string using fgets().

My lecturer advised this method:

char names[20];
fgets(names,20,fp);
names[strlen(names)-1]='\0';

I've omitted some of the code as I'm only demonstrating the method.

However, in our task, we have to deal with arrays of strings. I've tried...

names[strlen(names[i])-1]='\0';

but seems to only find the length of the string, deduct 1 and then use that number in the index which then sets a string further down the array to \0

Is there a way to access the individual characters of the strings?

I know I can access the strings using

names[i] // where i is the numeric index

but I need to access the individual characters within that string.

This is my first time posting on StackOverflow so please inform me if I haven't included enough detail or have formatted my question poorly.

Thanks in advance.

3
  • 1
    names[i][strlen(names[i])-1]='\0';, names[i][index] Commented Dec 29, 2013 at 19:56
  • @BLUEPIXY Post it as an answer. Commented Dec 29, 2013 at 20:00
  • Your teacher's code will not strictly remove a newline. See my comment in @erosenin's correct answer below if you are interested. Commented Dec 29, 2013 at 23:19

3 Answers 3

4

As noted, in C a string is an array of characters. So, you can access the individual characters like you access any array. For your task what you need to do is

  char names[100][20];
  //Now this declares 100 strings of size 20 each.
  //To access a single character in the position '4' from string 0 you can write
  printf("%c",names[0][3]);

  //To modify the string in position 'i' you will use 
  names[i][strlen(names[i])-1]='\0';

The last line is very similar to what you have written for a single string.

With the first index 'i' you access the string in that position. and with the second index you access a particular character of that string.

Sign up to request clarification or add additional context in comments.

4 Comments

I considered a 2D Array but wasn't sure which way to approach it in the question but thanks to your help, I attempted it and it's worked perfectly! Thank you.
No problem! Glad, I could be of help.
Solid answer. This is not a critique of your answer; more for OP. To treat the input data with a bit more care, I would suggest a strlen comparison or content check before assuming the len-1 position contains a newline. You can end up losing a letter of a 19-character name unnecessarily this way. Sure, it only truncates one letter in the worst case, but it can be prevented. Cheers.
I should clarify. I'm referring to behaviour of the fgets function -- it does not guarantee a newline. OP's question was using fgets to fill the names in.
1

Try this:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char names[10][10];
    char c = 'A';
    for(int index = 0; i < strlen(names) - 1)
    {
        names[0][index] = c++;
    }
    names[i][strlen(names[i])-1]='\0';

    printf("%s", namws[0]);

    return 0;
}

4 Comments

I'm sorry but this doesn't answer the question. See BLUEPIXY's comment on the question.
@H2CO3; I think he is talking about 1D array.
No, he isn't. "However, in our task, we have to deal with arrays of strings. [...] but seems to only find the length of the string, deduct 1 and then use that number in the index which then sets a string further down the array to \0" - that's quite the description of the problem arising when one confuses an array of strings with an array of characters.
@H2CO3; Mention not :)
0

In c, string is actually an array of char. So,

char[] charArray = "string";

charArray[0] will give you 's'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.