Sunday, 21 January 2018

Introduction To Strings


Introduction To Strings


Introduction To Strings
The way a group of integers can be stored in an integer array, similarly a group of characters can be stored in a character array . Character array are often called strings.A string constant is a one-dimensional array of characters terminated by a null('\0').The ASCII value of ('\0') is 0.

 Each character in the array occupies one byte of memory and the last character is always'\0'.
  The following example illustrates how a string is stored and accessed
main()
{
 static char name[]="Nagpur";
 int i=0;
 while (name[i]!='\0')
 {
  printf("%c",name[i]);
  i++;
 }
}
  A string can also be initialized as
      static char[]="Nagpur";

    Here '\0' is not necessary . C inserts it automatically.The terminating null('\0') is important,because it is the only way the functions that work with a string can know where the string ends.

Declaring and Initializing string variables:
         A string variable is any valid c variable name and is always declared as an array .The general form of declaration of a string variable is: char string_name [size];
   The size determines the number of characters in the string_name.The size should be equal to the maximum number of  characters in the string plus one.'C' permits a character array to be initialized in either of the following two forms
   static char city[9] = "Newyork";
   static char city[9] = {'N','E','W','Y','O','R','K','\0'};

Related Posts:

  • Program for Reverse Number#include<stdio.h>#include<conio.h>main(){ int a,r,rev=0; clrscr(); printf("enter the value of a"); scanf("%d",&a); while(a!=0) { r=a%10; rev=rev*10+r; a=a/10; } printf("The reverse no of the given no is=%d"… Read More
  • Program for Matrix Multiplaction | Sample program for Matrix |#include<stdio.h>#include<conio.h>main(){ int a[10][10],b[10][10],c[10][10],i,j,m,n,o,p,k; clrscr(); printf("input the order of A&B matrice's\n"); scanf("%d%d%d%d",&m,&n,&o,&p); if(n==o) { pri… Read More
  • Program for Flyorid Traingle/*floyd triangle*/#include<stdio.h>#include<conio.h>main(){ int i,j,t=1,n; clrscr(); printf("enter the value of n"); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { printf("%3d",t); t=t+1; }… Read More
  • Program for Factorial /*factorial*/#include<stdio.h>#include<conio.h>main(){ int i,n,f=1; clrscr(); printf("enter the value of n"); scanf("%d",&n); for(i=1;i<=n;++i) { f=f*i; } printf("%d",f); getch();} … Read More
  • Program for FIBANOCHI #include<stdio.h>#include<conio.h>main(){ int a,i=0,f0=0,f1=1,f; clrscr(); printf("enter the value of a"); scanf("%d",&a); printf("%d\n",f0); printf("%d\n",f1); do {       f=f0+f1; printf(… Read More

0 comments:

Post a Comment