Saturday 19 October 2013

Basic Questions-1 (string operation)

Basically we are going to solve a simple problem :

Write a PRogram to take input 3 student name & there roll no.  after that it ask for a roll no if it is found it print name of student associated with that roll no.

IT is very simple use header file "string.h" & use gets() & puts() function to take input string & print string.
& use strcmp() function to compare strings.

NOTE : strcmp() function return value 0 if comparison successful else it return an integer value that is equal to ASCII value difference between first non matching character encounter.
EXAMPLE : if we compare string "beginner" & "beginer" first none matching character is  'n' & 'e' so
  strcmp() return integer value = ASCII value of  'n' - ASCII value of  'e'  .

C program :



 #include<string.h>  
 #include<stdio.h>  
 main()  
 {  
    char name[3][20],roll[3][10],uroll[10];  
    int i=0,j=0;  
    for(i=0;i<3;i++)  
    {  
    printf("\n input %dth student name= ",i+1);  
    gets(name[i]);  
    printf("\n enter his roll no :");  
    gets(roll[i]);  
    }  
    printf("\n input std roll to find out his/her name=");  
    gets(uroll);  
    j=0;  
    for(i=0;i<3;i++)  
    {  
     if(strcmp(roll[i],uroll)==0) // if both are same strcmp return 0;  
      break;           // else return diff between ascii volue of first character does not match  
     j++;  
    }  
    if(j=3)//if student not found  
    {  
     printf(" student with this rool no not found ");  
    }  
    else  
    {  
    printf("\n name of student is : ");  
    puts(name[j]);  
    }  
 }  

OUTPUT : 
string operation

No comments:

Post a Comment

THANKS FOR UR GREAT COMMENT

Blogger Widgets