How to convert a Char String, storing in an Integer vector in C? [closed]

-4

How to convert a Char String and add to an Integer vector?

    
asked by anonymous 16.10.2017 / 23:06

1 answer

1
Basically what you should do is a technique known as casting, for you to tell the computer that a certain type of data should be interpreted as the data that you are doing casting, putting in other words you force a given type A is interpreted by the computer as a type B data.

To do casting is quite simple, you simply indicate the data you want it to be interpreted in front of the variable you want, it is very important that you place both the type and the variable in parentheses, to prevent the interpreter interpreter wrong, an example, let's suppose you have a data of type float in a variable called X and you want to assign this data to a variable B , only as an integer, to do this, just do the casting, then the response would go out in such a way B = (int)(X); .

To facilitate your understanding I recommend here a youtube video that teaches you to casting, as well as a IME theme (USP) on the subject.

Going straight to the answer to your question, but I recommend that you first see the video and the article, and also practice / try to do it yourself, but without further ado here is the answer to your question.

#include <stdio.h>
#include <locale.h>

int main ()
{
    char strVar[] = "ABCabc";
    int vet[10];
    register int i;

    setlocale(LC_ALL, "");

    for ( i = 0; strVar[i] != '
#include <stdio.h>
#include <locale.h>

int main ()
{
    char strVar[] = "ABCabc";
    int vet[10];
    register int i;

    setlocale(LC_ALL, "");

    for ( i = 0; strVar[i] != '%pre%'; ++i )
        vet[i] = (int)(strVar[i]);

    printf("O que há escrito na string: %s.\n"
           "Seus respectivos em inteiro (tabela ASCII):\n", strVar);
    for ( i = 0; strVar[i] != '%pre%'; ++i )
        printf("%i = %c\n", vet[i], vet[i]);

    return 0;
}
'; ++i ) vet[i] = (int)(strVar[i]); printf("O que há escrito na string: %s.\n" "Seus respectivos em inteiro (tabela ASCII):\n", strVar); for ( i = 0; strVar[i] != '%pre%'; ++i ) printf("%i = %c\n", vet[i], vet[i]); return 0; }

A little tip, always when doing a post here in stcak overflow is very specific with what you want, in addition it is very interesting that you show what you have managed to do so far, so we can tell you exactly where your error, and also give a better answer to your question, also show the output you are giving, possible error messages or warnigs and etc.

I hope to have helped, good luck with the learning gives C language bro:).

    
17.10.2017 / 12:59