marq

Dr. Charles Simonyi is the Father of Modern Microsoft Excel                                           JavaScript was originally developed by Brendan Eich of Netscape under the name Mocha, later LiveScript, and finally renamed to JavaScript.                                           The word "Biology" is firstly used by Lamarck and Treviranus                                           Hippocrates (460-370 bc) is known as father of medicine.                                           Galene, 130-200 is known as father of Experimental Physology                                           Aristotle (384-322 BC) is known as Father of Zoology because he wrote the construction and behavior of different animals in his book "Historia animalium"                                           Theophrastus(370-285 BC) is known as father of Botany because he wrote about 500 different plants in his book "Historia Plantarum".                                           John Resig is known as Father of Jquery -                                          HTML is a markup language which is use to design web pages. It was invented in 1990 by Tim Berners-Lee.                                                                The Google was founded by Larry Page and Sergey Brin.                                                                Rasmus Lerdorf was the original creator of PHP. It was first released in 1995.                                                               Facebook was founded by Mark Zuckerberg                                                               Bjarne Stroustrup, creator of C++.                                                                Dennis Ritchie creator of C                                                                                                                              James Gosling, also known as the "Father of Java"                                          At 11.44%, Bihar is India's fastest growing state                                          Father of HTML -Tim Berners Lee                                          orkut was created by Orkut Büyükkökten, a Turkish software engineer                    Photoshop: It came about after Thomas Knoll, a PhD student at the University of Michigan created a program to display grayscale images on a monochrome monitor which at the time was called 'Display'.

String handling in C


String handling in C

  • what is a string?
  • A string is combination of characters.
  • Any set or sequence of characters defined within double quotation symbols is a constant string.
  • In c it is required to do some meaningful operations on the strings


Initializing Strings
The initialization of a string must the following form which is simpler to the one dimension array
char month1[ ]={‘j’,’a’,’n’,’u’,’a’,’r’,’y’};


The following example shows the use of string:
/*String.c string variable*/
#include < stdio.h >
main()
{
char month[15];
printf (”Enter the string”);
gets (month);
printf (”The string entered is %s”, month);
}
Note:
Character string always terminated by a null character ‘’. A string variable is always declared as an array & is any valid C variable name. The general form of declaration of a string variable is


Reading Strings from the terminal:
The function scanf with %s format specification is needed to read the character string from the terminal itself. The following example shows how to read strings from the terminals:
char address[15];
scanf(%s,address);


String operations (string.h)
language recognizes that strings are terminated by null character and is a different class of array by letting us input and output the array as a unit. To array out many of the string manipulations,C library supports a large number of string handling functions that can be used such as:
1. Length (number of characters in the string).
2. Concatentation (adding two are more strings)
3. Comparing two strings.
4. Substring (Extract substring from a given string)
5. Copy(copies one string over another)


strlen() function:
This function counts and returns the number of characters in a particular string. The length always does not include a null character. The syntax of strlen() is as follows:
n=strlen(string);
Where n is the integer variable which receives the value of length of the string.


The following program shows to find the length of the string using strlen() function
/*writr a c program to find the length of the string using strlen() function*/
#include < stdio.h >
include < string.h >
void main()
{
char name[100];
int length;
printf(”Enter the string”);
gets(name);
length=strlen(name);
printf(”\nNumber of characters in the string is=%d”,length);
}
strcat() function:
when you combine two strings, you add the characters of one string to the end of the other string. This process is called as concatenation. The strcat() function is used to joins 2 strings together. It takes the following form:
strcat(string1,string2)
string1 & string2 are the character arrays. When the function strcat is executed string2 is appended to the string1. the string at string2 always remains unchanged.


strcmp function:
In c,you cannot directly compare the value of 2 strings in a condition like if(string1==string2) Most libraries however contain the function called strcmp(),which returns a zero if 2 strings are equal, or a non zero number if the strings are not the same. The syntax of strcmp() is given below:
Strcmp(string1,string2)

strcmpi() function
This function is same as strcmp() which compares 2 strings but not case sensitive.
Strcmp(string1,string2)


strcpy() function:
To assign the characters to a string,C does not allow you directly as in the statement name=Robert; Instead use the strcpy() function found in most compilers the syntax of the function is illustrated below.
strcpy(string1,string2);


strlwr () function:
This function converts all characters in a string from uppercase to lowercase
The syntax of the function strlwr is illustrated below
strlwr(string);


strrev() function:
This function reverses the characters in a particular string. The syntax of the function strrev is illustrated below
strrev(string);


write program to print name in reverse

#include < stdio.h >
#include < string.h >
void main()
{
char nm[20];
int i,len=0;
clrscr();
printf("Enter the name :");
gets(nm);
for(i=len;i>=0;i--)
{
printf("%c",nm[i]);
}
getch();
}


Dispay a string in increasing order.
#include < stdio.h >
#include < string.h >

void main()
{
char st[20];
int len=0,i,j;
clrscr();
printf("Enter a string");
gets(st);
len=strlen(st);
for(i=0;i<len;i++)
{
for(j=0;j<i;j++)
{
printf("%c",st[j]);
}
printf("\n");
}
getch();
}


Display the sum of ascii of character;
#include < stdio.h >

#include < string.h >
void main()
{
char st[20]
int i,n,len=0,sum=0;
clrscr();
printf("Enter String");
gets(st);
len=strlen(st);
printf("The sum of ascii of character is : ");
for(i=0;i<len;i++)
{
printf("%d+",st[i]);
sum=sum+st[i];

}
printf("=%d",sum);
getch();
}

Remove duplicate Entry in Array
#include < stdio.h >
#include < string.h >
void main()
{
   int a[10],i,n,j,flag=1;
   clrscr();
   for(i=0;i<10;i++)
     {
      printf("Enter array elements a[%d] :"i);
       scanf("%d",&a[i]);
     }

  for(i=0;i<10;i++)
    {
    flag =1; 
    for(j=0;j<i;j++)
      {
        if(a[i]==a[j])
          {
          flag=0;
          }
     }
        if(flag==1)
         {
        prinf("\n%d",a[i]);
         }

 }
getch(); 
}



The following program illustrate the use of string functions:
/* Example program to use string functions*/
#include < stdio.h >
#include < string.h >
void main()
{
char s1[20],s2[20],s3[20];
int x,l1,l2,l3;
printf(”Enter the strings”);
scanf(”%s%s”,s1,s2);
x=strcmp(s1,s2);
if(x!=0)
{printf(”\nStrings are not equal\n”);
strcat(s1,s2);
}
else
printf(”\nStrings are equal”);
strcpy(s3,s1);
l1=strlen(s1);
l2=strlen(s2);
l3=strlen(s3);
printf(”\ns1=%s\t length=%d characters\n”,s1,l1);
printf(”\ns2=%s\t length=%d characters\n”,s2,l2);
printf(”\ns3=%s\t length=%d characters\n”,s3,l3);
}

No comments:

Post a Comment