facebook like button

02 April, 2011

program 25: characters and strings

The need:
     In all my previous posts I have told you about some data-types in C(int, float, double etc.) but all these are data-types for numbers. We know that numbers are not the only thing which we use in our daily life. Words and letters also equally important. Don't you think that a program should have data-type for those? Yes there is a data-type char for these words and letters. Here char stands for "character". For now just remember that in C we use term "character" for letters(alphabet), single digit or any other symbol on keyboard. This program shows how we scan characters and strings of characters (words and phrases in normal English).This program only takes the input and prints back.

The code: 
--------------------------------------------
#include<stdio.h>
int main()
{
    char j,k;              // declaration of single character
    char s[15],t[40];     //declaration of character strings
    printf("Enter any character\n");
    k=getchar();
    printf("You entered\t %c \n",k);
    printf("This character was stored in variable k\n\n");
    printf("Enter your first name...\n");
    scanf("%s",s);         // scanning s as string
    printf("Your first name is:\t %s\n",s);
    j=getchar();
    if(j=='\n')
    printf("j is ENTER\n\n");
    else
    printf("j is %c\n",j);
    printf("Enter your full name\n");
    gets(t);            // getting t as string
    printf("\nYour full name is %s\n\n",t);
    printf("Still the value stored in variable k is\t");
    putchar(k);
    printf("\n\n");
    return 0;
}
-----------------------------------------

The approach:
     First of all run the program, then try to understand the code. Read the explanation below thoroughly. In case of any doubt you can ask me on may email or just comment on this post because this is an important program to learn strings.


     In first line of main() char is used to declare 2 character variables j and k. This means these variables can store the one character each. In the second line char is used to declare strings variables. The numbers in square brackets indicate the maximum length of the string( maximum possible number of characters in that string). The function getchar() reads single character from keyboard and assign that character to the variable written on left side of it. In next line printf is used to print value of character variable k. Here %c is signifies that the value to be printed is a character(like %d signifies an integer). 


 In 8th line of main() scanf() is used to read a string of characters and assign that string to string variable s. For giving a string as input you have to hit ENTER key after that. If you want program to give desired result, don't give your first name with a space because scanf() stops reading a string whenever it encounters a space. Observe there is no & operator before s in this scanf statement. When we scan strings, we don't use & operator. Then next line printf() prints the first name. After that there is a getchar() function which assigns any character taken from keyboard to j. You are thinking that the program should ask you as I told before for but if you run the program, it doesn't demand a character as input. The reason for that is simple. Recall you just hit the ENTER key after your first name. This ENTER key is also a character. Your first name goes into s. Where does the ENTER key goes. It is not lost. This is used by the getchar() and assigned to j as a character literal(a character is also a literal).


    You can see that to verify this I have put an if else statement in this program. If j is ENTER or '\n', program will print "j is ENTER" otherwise character value of j will be printed whatever it is. Now the program again asks you for your full name. To read this I have used gets() function here. The gets function allows the spaces to be taken as a part of the given string. It scans anything in between until it finds the ENTER key. So here you can give your full name with spaces in between. The whole name is assigned to string variable t. This full name gets printed in the next printf() line. Recall that the variables store the value permanently until it is changed. To demonstrate another function putchar() to print a single character, I have again printed the value of k.

Remark:
     All of beginners are advised to practice 2 or 3 more programs of your own.

2 comments:

  1. Hi,

    I have a question.
    I have read that when declaring an array of integers or strings we don not need to specify the no. of elements it takes.
    Like this: int a[]; or char a[];

    What does this mean?
    In this case how many elements can the array take?
    Please explain.
    Thanks

    ReplyDelete
    Replies
    1. No. As far as I know we can't declare an array without size in C. Yes while declaring a function, if we pass a one dimensional array as an argument to that function, we don't need to give size with it.

      Delete

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply