C PROGRAMMING

 

S

tring:- String is a collection of character type data. In other words character arrays are called string. In C programming the group of characters,digits, and symbols enclosed within quotation marks are called as string.Every string is terminated with string ‘\0’(NULL) character. The Null character is a byte with all bits at logic zero. Hence its decimal value is zero. It is not compulsory to write ‘\0’ in string. The compiler automatically puts ‘\0’ at the end of the character array(string). Every character of string store in contiguous memory locations. Each  character of string occupies 1 byte of memory. 

char nm[5];

 A

     B

     H

      I

     \0

   nm[0]          nm[1]             nm[2]         nm[3]           nm[4]

 

or

char name[ ]= {‘I’, ‘N’ ,’ D’ , ’I’ , ‘A’ ,’\0’};

char text[]= “HAVE A NICE DAY”;

  (Initialization of NULL character is not essential.)

character array can be initialized as follows:-

(a)          char name[6]={‘p’ , ’a’ , ’n’ , ’k’ ,’a’ , ’j’ };

(b)         char name[6]={‘p’ , ’a’ , ’n’ , ’k’ ,’a’ , ’j’ };

Here , in case (a) the output will not be ‘pankaj’ but it contains some garbage value. Arguments in this example are initialized with 6, which is exactly equal to a number of characters inside the braces. The Null character must be included and hence, the argument must be [7] instead of [6].

Example:-

#include <stdio.h>

#include<conio.h>

void main()

{

  char name1[6]={‘p’ , ’a’ , ’n’ , ’k’ ,’a’ , ’j’ };

  char name2[7]={‘p’ , ’a’ , ’n’ , ’k’ ,’a’ , ’j’ };

clrscr();

printf(“name1=%s”,name1);

printf(“name2=%s”,name2);

}

Output :- pankajpankaj

                pankaj

Here null character has not been considered in the first statement.

Declaration of string in different formate:-

char arr1[9]={‘W’, ‘E’, ‘L  ’, ‘   ’, ‘C  ’, ‘ O’, ‘ M’, ‘ E ’, ‘ \0 ’ }

char arr2[9]=”WELCOME”

char arr3[9]={{‘W’},{‘E’},{‘L’},{‘ ’},{‘C’},{‘O’},{‘M’},{’E’}};

printf(“\n Array1=%s”,arr1);

printf(“\n Array2=%s”,arr2);

printf(“\n Array3=%s”,arr3);

(Here, above all print welcome)

 

*Display of strings:-

The printf() function with %s format is to be used for displaying the string on screen.

printf(“%s \n”,nm );    PRABHAKAR

printf(“%.5s \n”nm); prabh.

 

*** How to read string form terminal***

#include<stdio.h>

int main()

{

  char name[20];

  printf(“Enter Name”);

  scanf(“%s”,name);

  printf(“your name is %s”,name);

  return 0;

}

                Output:-

                Enter name: Abhishek kumar.

                Your name is: Abhishek

(Here program will ignore kumar because, scanf() takes only string before the white space).

//Prg to reading a line of text*/

#include<stdio.h>

#include<conio.h>

int main()

{

  char name[30],ch;

int i=0;

printf(“\n Enter name”);

  while(ch!=’\n’)

  {

    ch=getchar();

      name[i]=ch;

     i++;

   }

    name[i]=’\0’;

    printf(“Name=%s”,name);

    return 0;

}

 

(Here , The process to take string is tedious. There are predefined function gets() and puts in c language to read and display string respectively.)

 

#include<stdio.h>

#include<conio.h>

  int main()

{

   char name[30];

   printf(“Enter name”);

   gets(name);//Function to read string from user.

   printf(“Name:-”);

   puts(name);//Function to display string.

   return (0);

}

(Both above program has same output.)

        Enter name: Abhi kr.

         Name:-          Abhi kr.



//**There are some example(program) related with string.

//prg to count white space in given text

#include<stdio.h>

#include<conio.h>

int main()

{

char nm[20];

   int i,c=0;

  printf("Enter char ");

  //scanf("%s",nm);

  gets(nm);

  for(i=0;nm[i]!='\0';i++)

  {

    if(nm[i]=='  ')

      c=c+1;

  }

    puts(nm);

    printf("\n total white space= %d",c);

    getch();

    return 0;

 

}

 

//prg to count total length of  given text

#include<stdio.h>

#include<conio.h>

int main()

{

    char nm[20];

    int i,c=0;

    printf("Enter char ");

    //scanf("%s",nm);

    gets(nm);

    for(i=0;nm[i]!='\0';i++)

    {

        c=c+1;

    }

    puts(nm);

      printf("\n total white space= %d",c);

      getch();

      return 0;

}

 

//prg to count total vowel , consonent and white space

 

#include<stdio.h>

#include<conio.h>

int main()

{

    char nm[20];

   int i,vol=0,space=0,consonent=0;

  printf("Enter char ");

  //scanf("%s",nm);

  gets(nm);

  for(i=0;nm[i]!='\0';i++)

  {

    if(nm[i]=='a' || nm[i]=='A'|| nm[i]=='e' ||nm[i]=='E'||nm[i]=='o' ||nm[i]=='O'||nm[i]=='u'|| nm[i]=='U'||nm[i]=='i'||nm[i]=='I')

 

              vol++;

              else if(nm[i]==' ')

                space++;

              else

               consonent++;

 }

    puts(nm);

    printf("\n total Vowel= %d",vol);

    printf("\n total space =%d",space);

    printf("\n total consonent=%d",consonent);

    getch();

    return 0;

 }

 


Call by Value and Reference

There are two ways in which we can pass arguments to the function.

(1). Call by Value:-

 In this type value of actual arguments are passed to the formal arguments and the operation is done on the formal arguments. Any change made in the formal argument does not effect the actual argument because formal arguments are photocopy of actual arguments. Hence, when function is called by the call or value method , it does not affect the actual contents of the actual arguments. Changes made in the formal arguments are local to the block of the called function. Once control returns to the calling function the changes made vanish.

Now we try to understand through program.

#include<stdio.h>

#include<conio.h>

int main()

{

void swap(int,int);

clrscr();

int x,y;

printf("\n enter value of x and y");

scanf("%d %d",&x,&y);

swap(x,y);//Actual Argument

printf("\n in main() X=%d y=%d",x,y);

return 0;

}                                               

void swap(int a,int b)//Formal Argument

{

  int k;

  k=a;

           a=b;

                   b=k;

  printf("\n  change function x=%d y=%d",a,b);

}

 

                                                outpur:-

                                                Enter values of x and y: 10   20

                                                After swap        x=20   y=10

                                                in main              x=10    y=20


(2)Call By reference:-

                                      In call by reference instead of passing values, addresses(reference)are passed. In call by reference method  function operates on addresses rather than values. Here formal arguments are pointers to the actual arguments and formal arguments points to the actual argument. Hence changes made in the arguments are permanent.

//Write a program to send a value by reference.

#include<stdio.h>

#include<conio.h>

int main()

{

  int x,y;

 

  swap(int* , int*)  ;

  clrscr();

  printf("\n Enter values of x and y");

  scanf("%d %d",&x,&y);

  swap(&x,&y);

  printf("\n in main() x=%d y=%d",x,y);

 return 0;

}

 

 

swap(int *a , int *b)

{

  int *k;

  *k=*a;

  *a=*b;

  *b=*k;

  printf("\n in swap() x=%d  y=%d",*a,*b);

 

}

 

Output:-

Enter no in x & y. 10         20

In  swap()          x=20          y=10

In  main()          x=20          y=10.

 

 

Pointer:-

 

Variable are used in C to hold data values during the execution of a program. Every variable when declared occupies certain memory location. In c it is possible to access and display the address of memory location of variable using & operator with variable name.

 

Pointer is a memory variable that stores a memory address of another variable. Pointer name is as same as variable name but it always denoted or preceded by “*” operator.

 

Advantage of pointer.

 

1. (  Pointers save the memory space.

2.    Pointer is faster because data is manipulated with the address i.e. direct access to memory location.

3.   The memory is accessed efficiently with the pointer

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Comments

Popular posts from this blog

DATA STRUCTURE

Microsoft Visual Foxpro

Visual Basic 6.0