Sunday 12 December 2010

Program that displays the position or index in the string S where the string T begins , or -1 if S doesn’t contain T

Program:


#include<stdio.h>
#include<string.h>
#include<conio.h>

void main()
{
 char s[30], t[20]; char *found; clrscr();

/* Entering the main string */ puts("Enter the first string: "); gets(s);

/* Entering the string whose position or index to be displayed */ puts("Enter the string to be searched: ");
gets(t);

/*Searching string t in string s */      
  found=strstr(s,t);
 if(found)
printf("Second String is found in the First String at %d position.\n",found-s);
 else
 printf("-1");  
 getch();
}

Output:


1.enter the first string: kali
Enter the string to be seareched: li
  second string is found in the first string at 2 position
2.enter the first string:
nagaraju
    Enter the string to be seareched:
    raju
    second string is found in the first string at 4 position 3.enter the first string:
   nagarjuna
   Enter the string to be seareched: ma
-1

Conclusion: The program is error free


VIVA QUESTIONS:

1)  What is the difference between printf() and puts() ?
Ans: puts() is used to display the string at a time and it doesn’t take any integers values but printf() takes any values as defined by the user

2)  define pointer variable ?
Ans: pointer variables are defined as variable that contain the memory addresses of data or executable code.

3)  What is use of the strcmp() function ?
Ans: This function compares two strings character by character and returns a value 0 if both strings are equal and non zero value if the strings are different.





c programs

No comments:

Post a Comment