Search code examples
cstringiterator

Iterate through every char in string stored in an array


I am really new to C and in my first half year at university. This is my first questio on StackOverflow.

My task is to program it so every string stored in numbers is being converted into a decimal, without changing anything outside the main function.

I am now trying for the past 4 hours to solve this problem, where I want to iterate trough every char in the string I am currently to then, based on there position in comparison to the length to convert it into a decimal.

My only question here is to someone help me to understand how I can get the string length without using strlen() due to the fact I can't add #include <string.h>

This is what I got so far (getting the length of the array to iterate through every index):

#include <stdio.h>

#include <math.h> // Kompilieren mit -lm : gcc -Wall -std=c11 dateiname.c -lm

int main() {
    char* numbers[] = {
        "01001001",
        "00101010",
        "010100111001",
        "011111110100101010010111",
        "0001010110011010101111101111010101110110",
        "01011100110000001101"};
    
    // Add here..
    
    int length = sizeof(numbers);
    
    

    for ( int i = 0; i < length; i++ ){
        
         //how do i get the string size without strlen() D:
            
        }
        
    
    
    return 0;
}

Solution

  • In C, strings are really just char arrays with a special terminator character to mark the end of the string. So, say you have something like:

    char *str = "hello";
    

    This is essentially equivalent to this:

    char str[] = {'h', 'e', 'l', 'l', 'o', '\0'};
    

    Notice that \0 character at the end of the array? This is the special terminator character that C places at the end of strings. Functions like strlen() pretty much iterate through the char array looking for the first occurrence of the \0 character and then stopping.

    So, you can make your own version of strlen(), say my_strlen() like this:

    int my_strlen(char *str)
    {
        /* Initialize len to 0 */
        int len = 0;
        
        /* Iterate through str, increment len, and stop when we reach '\0' */
        while(str[len] != '\0')
            len++;
        
        /* Return the len */
        return len;
    }
    

    Then within your for loop, you can just call this function. Also, note that your calculation of the size of the numbers array:

    int length = sizeof(numbers);
    

    will not give you the number of elements in the array. That code gives you the size (in bytes) or numbers which is an array of char pointers. If you want to get the number of elements, you have to divide that size by the size (in bytes) of a single element (i.e., a char pointer). So, something like this would work:

    int length = sizeof(numbers) / sizeof(numbers[0]);
    

    Your final code can look something like this:

    #include <stdio.h>
    
    #include <math.h> // Kompilieren mit -lm : gcc -Wall -std=c11 dateiname.c -lm
    
    int my_strlen(char *str) {
        /* Initialize len to 0 */
        int len = 0;
        
        /* Iterate through str, increment len, and stop when we reach '\0' */
        while(str[len] != '\0')
            len++;
        
        /* Return the len */
        return len;
    }
    
    int main() {
        char* numbers[] = {
            "01001001",
            "00101010",
            "010100111001",
            "011111110100101010010111",
            "0001010110011010101111101111010101110110",
            "01011100110000001101"};
        
        // Add here..
        
        // Notice the change here
        int length = sizeof(numbers) / sizeof(numbers[0]);
        
        
        for(int i = 0; i < length; i++ ){
            int str_len = my_strlen(numbers[i]);
            // Do what you need with str_len            
        }
            
        
        return 0;
    }