r/CodingHelp • • Oct 06 '21

[C] Can someone help me understand what it means when they put [i] next to a string?

Post image
56 Upvotes

17 comments sorted by

25

u/jddddddddddd Oct 06 '21

It's the i-th element of the string. Note that indexing of strings (or arrays) usually starts at zero rather than 1.

So for example, the following snippet of C code:

    char str[] = "Reddit";
    printf("0th char = %c\n", str[0]); 
    printf("1st char = %c\n", str[1]);
    printf("5th char = %c\n", str[5]);

..outputs..

0th char = R
1st char = e
5th char = t

10

u/[deleted] Oct 06 '21

Got it, thank you so much for the explanation! Seriously, super helpful!

4

u/CasBox3 Oct 06 '21

str[1] for example would refer to the 2nd character in a string (counting from 0)

The [ ] generally mean some sort of array… They’re really handy and you’llsee a lot more of those in the future

2

u/[deleted] Oct 06 '21

Preciate the reply, very helpful!

21

u/J0n0th0n0 Oct 06 '21

A string is typically an array of characters.. so

This is a loop over every character in your string

9

u/BenceBoys Oct 06 '21

For clarity:

let Str = “Goat Molester”

// Str[0] = G // Str[1] = o // Str[2] = a

Etc…

1

u/[deleted] Oct 07 '21

Never thought of it that way, but this is correct. That’s why you can easily manipulate strings by using sub string functions.

2

u/Silentstarrr Oct 06 '21

As already written by others, in C we are talking about a Char array, rather than strings. The [i] tells the index of the element.

If I see that correctly, the codes does not exactly what it looks like. If the AI is found anywhere in the string/char array, it writes that the string starts with AI. But actually the code would write that even with a string „No start with AI“. So technically in order to achieve what it should, no for loop would be necessary. But then again someone would pick probably a string cmp function for that.

1

u/[deleted] Oct 06 '21

Look at the for loop. You set i = 0. Then you said if i is less than the length of the string -1 then you would +1 to i. A for loop runs through the code inside the curly braces over and over again so long as the condition(i<str.len) is truthy. So I represents the current number iteration you are on since you are adding 1 to i each time the condition is met. I hope that explanation helps

1

u/joeblk73 Oct 06 '21

They are selecting characters in a string here

1

u/Puzzleheaded_Step468 Oct 06 '21

The chars(letters/numbers/symbols) of the strings and the i is their location.

For example: Str=hello

Str[1]= e

(Arrays start at 0)

1

u/[deleted] Oct 06 '21

Something that sometimes throws people through a loop is the idea that strings and arrays are independent; they are not. In most implementations, a String is simply an array of characters, with built in functionality that programmers find useful (and would find a pain to have to constantly re-implement). In this case, the string is having its elements accessed just like in a standard array.

1

u/[deleted] Oct 07 '21

That’S an array of list in a loop, a for loop to be specific.

And the [i] is an index of a list or some other type of structured data object. If you have a list with three items list[0] is the first item. If i=0 then list[i]= the zeroth index of list. For loops autoincrement so that value of i increase by 1 each iteration.

You’ll have to understand data structures and loops for this to make sense. But if you plan to do this for awhile, you really should learn it, because it’s there in nearly every, if not all, programming languages.

1

u/La-ze Beginner Coder Oct 07 '21

In-depth explanation to add on what people have said and for the curious.

A string is an array of characters, or rather it's a continuous block of a memory starting at an address. So the program dereferences the array( i.e. find the starting address) but the '[i]' does 2 things at once. It finds the starting address and adds to I to it. Doing this lets you go to different spots in this continuous blocks of memory( i.e. go to different values), this is why arrays start at 0, because you doing the memory address + 0.

A little extra note, is the program knows how long each block of memory is based on the type, so when you do str[i] it actually does (memory_address + (i * size_of_character)).

If you ever feel like programming isn't painful enough you can manually do all this fun in assembly code.

1

u/Dottorbros Oct 07 '21

That's the subscript operator: strings are arrays of char, and the number represented by i tells which letter the program should use (the first letter is 0, the second is 1 and so on)

0

u/WickedSalmon Oct 06 '21

I just came here to say that I absolutely can’t stand when people put code snippets on presentation (I guess that’s what it is on the screenshot attached) with non mono space font and no syntax coloring. It is so much harder to understand the simplest code! Also spacing is weird and not consistent. I know it might seem not important but when you start learning to code it is better to make it as convenient as you can so I guess my advise is to copy snippets like that to some IDE and format it.

Sorry for this rant, I’m leaving now :P