Print a character in place of a number

3
#include<stdio.h>
#include<string.h>

int main()
{
    char str[50];
    int i, l = 0;

    printf(" We will count the number of letters\n");
    printf("-------------------------------------\n");

    printf("Tell me the word: \n");
    scanf("%s", str);

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

int main()
{
    char str[50];
    int i, l = 0;

    printf(" We will count the number of letters\n");
    printf("-------------------------------------\n");

    printf("Tell me the word: \n");
    scanf("%s", str);

    for( i= 0; str[i] != '%pre%'; i++){
        l++;
        printf("The letter %d is %d\n", i, str[i]); /* não entendi por que é
        mostrado o valor de cada letra mas não a letra em si */
    }
    printf("|The number of words is: %d\n", l);
    return 0;

}
'; i++){ l++; printf("The letter %d is %d\n", i, str[i]); /* não entendi por que é mostrado o valor de cada letra mas não a letra em si */ } printf("|The number of words is: %d\n", l); return 0; }

ThisisanactivityIfoundontheinternetwhereIhavetofindthesizeofastringwithoutusingthelibraryfunction.WhatIdidnotunderstandisasacommentinthecode.IwouldalsoliketoknowwhywhenIput"\ 0" instead of '\ 0' the program does not work: it goes into an infinite loop.

    
asked by anonymous 22.12.2016 / 00:16

3 answers

3

You're showing the numeric value, not the character because that's what you had it done. The %d command takes a value and prints as a decimal number. If you use %c you are prompted to print the same value as a character. printf() is a form of presentation, you tell how you want the values to appear. You have to choose the format that fits your needs.

C is a weakly typed language, so you can access a value any way you like.

"'"%code%"'" is a string , %code% is a character. You must compare a character to another character, it can not compare to string which is actually a string ending with a null, ie %code% , so %code% are actually two characters is the %code% that is within the quotation marks plus another %code% that is the terminator of the string , which does not even make sense to have, since the first terminator already encloses the string , however, masters are put there for the sake of consistency.

What this condition is doing is just looking for the character terminator to open that the string is over. Contrary to what you might imagine the string does not have 50 characters, it has so many characters until you find the terminator. It may end before age 50, or later, which will take up a space of unreserved memory and will probably bring problems. Note that if you do not want to pop the placeholder, your string can have a maximum of 49 valid characters, since the last one will be reserved for the terminator.

#include<stdio.h>
#include<string.h>

int main() {
    char str[50];
    printf(" We will count the number of letters\n");
    printf("-------------------------------------\n");
    printf("Tell me the word: \n");
    scanf("%s", str);
    int i;
    for (i= 0; str[i] != '
#include<stdio.h>
#include<string.h>

int main() {
    char str[50];
    printf(" We will count the number of letters\n");
    printf("-------------------------------------\n");
    printf("Tell me the word: \n");
    scanf("%s", str);
    int i;
    for (i= 0; str[i] != '%pre%'; i++) {
        printf("The letter %d is %c\n", i, str[i]);
    }
    printf("|The number of words is: %d\n", i);
}
'; i++) { printf("The letter %d is %c\n", i, str[i]); } printf("|The number of words is: %d\n", i); }

See running on ideone and on CodingGround .

    
22.12.2016 / 00:29
2

A string is nothing more than a string, in C, this means an array of chars basically, and at the end the character '\ 0' is added. Note that it scans the input and places it in an array of 50-position chars, where the first will be filled with the words written + \ 0 to indicate the end of the string and the rest of the positions are filled with garbage values, that's the reason to have the '\ 0'.

Suppose the entry is "Test"

str [0] = 'T'; str [1] = 'e'; str [2] = 's'; str [3] = 't'; str [4] = 'e'; str [5] = '\ 0'; str [6] = garbage; . . . str [49] = garbage;

A difference between using double quotation marks and single quotation marks. Single quotes means that it is a char as double quotes which is a string. A char has a numerical value associated with the letter, this is in the ASCII table, if you use% d it will print the numeric value, the correct one is to change it to% c or% s.

printf("The letter %d is %c\n", i, str[i]);
    
22.12.2016 / 00:28
2

Why within printf (), your second% d is wrong . % d is just for printing numbers.

% c is for printing characters; and

% s is to print string.

Then the correct one would be: (did not compile)

printf("The letter %d is %c\n", i, str[i]); 

You were printing the ASCII table relative to those characters.

    
22.12.2016 / 00:59