I'm working on the pset2 Bulbs problem and I've come to a place where I don't know what to do next. This code takes user text input and converts it to ascii; I can see that the array is being filled as the loop iterates through the input text, but I can't access that array and return it outside of the loop. I want to have this array accessible for the next step of the problem. how can I pass the array out at the end of the loop (with return or something similar) to be able to pass it on to the next step of the problem?

#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

const int BITS_IN_BYTE = 8;

int letters_to_ascii(string sentence);
void print_bulb(int bit);

int main(void)
    // Get input text from user
    string sentence = get_string("Enter text here: ");

    int output = letters_to_ascii(sentence);

int letters_to_ascii(string sentence)
    int n = 0;
    int i = 0;
    int ascii[strlen(sentence)];

    while (sentence[n] != '\0')
        //printf("%d ",sentence[n]);
        ascii[i] = sentence[n];
        printf("%i ", ascii[i]);
    return n;

2 Answers 2


loop within loop. I am working on same problem but stuck at next step. cant figure out how to reverse each row. If you figure out do drop a hint.

  • Was trying the same because I could not figure out how to reverse it, no matter how many loops I did. C and lower level languages deal a lot with bits, you can look into the bitwise operators in C and see how you can extract the bits that way.
    – hmitrea
    Feb 16 at 5:19

You can treat sentence[i] as an integer without the need for a letters-to-ascii function. If you can handle one character at a time and print a row of bulbs, you can then place that inside a loop for each character. I was able to make progress this way.

// Get string
string msg = get_string("Your message: ");
int l = strlen(msg);

// For char in msg
for (int i = 0; i < l; i++)
    // Cast char to int
    int n = msg[i];

    // Find 8 bits

    // Print row of bulbs

    // End row, new line

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .