nAutAxH AhmAd nAutAxH AhmAd - 6 months ago 34
C Question

Strings in C Program

I am making a program in C language which prints the ASCII value of the string/text including spaces. The program works fine and give exact ASCII values of string including spaces but the problem is that it also prints '10' at the end of all ASCII values.

This is my code :

#include<stdio.h>
#include<conio.h>
int main()
{
char str[100];
int i;
printf("Enter a string: ");
fgets(str,100,stdin);
//scanf("%s",str);
printf("String is: %s\n",str);
printf("ASCII value in Decimal is: ");
for(i=0; str[i]!='\0'; i++)
{
printf("%d ",str[i]);
}
printf("\n");
getch();
}


Please help me out, tell me what's the problem and how to fix it. Thanks in advance.

Answer Source

When you read a string with fgets the enter you press is included in the string( man page ). ASCII code for \n is 10.