1

how to store Ascii character a to z in any one type of variable using strcpy?

for(i=97;i<122;i++)
{
    for(j=97;j<122;j++)
    {
        printf("%c%c",i,j);
        int a = strcpy(i,j);
    }
}
James M
  • 18,506
  • 3
  • 48
  • 56
Arsalaan Ahmed
  • 9
  • 1
  • 1
  • 2

5 Answers5

2
char a[3] = {0};

for(i=97;i<122;i++)
{
    for(j=97;j<122;j++)
    {
        printf("%c%c\n", i, j);
        a[0] = i;
        a[1] = j;
        printf("%s\n", a); 
    }
}
ouah
  • 142,963
  • 15
  • 272
  • 331
0

Ηow to store Ascii character in any one type of variable using strcpy()?

You don't need it, since for a single character strcpy() is unnecessary.

Moreover, you try to handle an int with strcpy(), whereas this function handles char*.


What you could do is something like this:

char myarray[3] = {0};
for(i = 97; i < 122; i++)
{
    for(j = 97; j < 122; j++)
    {
        printf("%c%c\n", i, j);
        myarray[0] = i;
        myarray[1] = j;
        printf("%s\n", myarray); 
    }
}

where I used a char array, since if you store the ASCII character in an int you cannot expect it to print the same character, as it was stored in a char. Even if you try Convert char array to a int number in C, you will not be able to succeed in it.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
0

So (assuming ASCII):

char str[27]={0};
char temp[2]={0};
int i;
for (i='a';i<='z';i++) {
 temp[0]=i;
 strcpy(str,temp);
}

But do you really need to use strcpy?

asaelr
  • 5,438
  • 1
  • 16
  • 22
-1

you can do it easily using type cast inside your loop

int num = 97;
char a = (char)num;   // 'a' = 97 casting int to char 
num = (int)a       // num = 97 cast 'a' to ASCII

let it try

Nikson Kanti Paul
  • 3,394
  • 1
  • 35
  • 51
-2
int main() {
    int a = 0;
    char c = '\0';
    scanf(" %c", &c);
    a = c + 0;
    printf("%d", a);
}

Output: ASCII value of entered character.

I have provided the the code. Please refere it. Why this is So? Focus on the addition operation which is assigned to the int variable a. During compilation of this operation compiler finds that the variable c is of char type but; the operator + is next to it and hence compiler uses the ascii value of the character stored in c variable.

I hope it will helpful for you. Thank you.

iehrlich
  • 3,572
  • 4
  • 34
  • 43
Andy.inamdar
  • 303
  • 1
  • 3
  • 10
  • Please post code as actual text, not images. Also, not only `+0` doesn't do anything here, but the `int a;` is not necessary. You could print `c` directly with `%d`. – HolyBlackCat Jul 09 '17 at 12:51
  • Please read the question carefully. The question is about to store the ascii value of the characters not to only print them. Ok? @HolyBlackCat I hope you will read the question again. – Andy.inamdar Jul 09 '17 at 17:23
  • I think I have provided the correct solution to store the ascii value. But don't know why my answer got two dislikes? – Andy.inamdar Jul 09 '17 at 17:27