-1

Basically I am trying to convert each letter of an array to a number, that is also an array, but trying to do it with a for and ascii, it is creating an infinite cycle, can you help?

I created a for with variable "I" until its letters array length, and tried to transform its characters array type char into an array int number, but it is not working properly.

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
char teste();
int main(int argc, char *argv[]) {
// inicializing variables
int i = 0, i2 = 0;
char b[]={};
int numero[] = {};
// reads its array "b"
scanf("%s",b);
// and writes it
printf("%s\n",b);
// writes the number which each letter of the array corresponds (What I wanted)
printf("%i", string_tamanho( b));
    for(i = 1; i<= string_tamanho(b); i++){
    numero[i] = b[i] - 96;
    printf("%i\t", numero[i]);
    }

}
int string_tamanho(char b[]) {
    // initializing conta variable (stores the length of the string)
    int conta; 
    
    // incrementing the count till the end of the string
    for (conta = 0; b[conta] != '\0'; ++conta);
    
    // returning the character count of the string
    return conta; 
}

Swampshot
  • 1
  • 1
  • 2
    Remember that arrays in C are not dynamic, and doesn't allow empty arrays. Both your arrays `b` and `numero` are defined as *empty* (with a zero size). If your compiler isn't already complaining about it, then you need to enable more warnings, and treat warnings as errors. – Some programmer dude Dec 07 '22 at 12:28
  • 1
    How big do you think the array created by the `char b[]={};` statement will be? Try adding an *explicit* size to the array and be sure not to exceed that length with the input you give. – Adrian Mole Dec 07 '22 at 12:28
  • calling `string_tamanho` on every loop is kind of inefficient – stark Dec 07 '22 at 12:51

1 Answers1

0

Try this, I cleaned up the code a little bit (get you move forward) where the for loop in string_tamanho iterates for MAX_STR number of times.

#include <stdio.h>
#include <stdlib.h>
#define MAX_STR 33;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
char teste();
int main(int argc, char *argv[]) {
// inicializing variables
int err;
int len=0;
char b[MAX_STR];
int numero[MAX_STR];
b[32]=0;
numero[32]=0;

// prompt for sequence of numbers, read this 
//  https://stackoverflow.com/questions/18372421/scanf-is-not-waiting-for-user-input
err =scanf("%s",b);

// and writes it
//  printf("%s\n",b);

// writes the number which each letter of the array corresponds (What I wanted)
len = string_tamanho(b);
printf("Length b>> %d", len );
    for(int i = 1; i< len; i++){
       numero[i] = b[i] - 96;
       printf("%d\t", numero[i]);
    }

}
int string_tamanho(char b[]) {
    // initializing conta variable (stores the length of the string)
    int conta=0;   
    
    // incrementing the count till the end of the string
    for (conta = 0; (b[conta] != '\0' && conta < MAX_STR); ++conta);
    
    // returning the character count of the string
    return conta; 
}
MZM
  • 105
  • 1
  • 3