Monday, 8 December 2014

C Program to find location of character in a string

Program to find character location in a string
#include<stdio.h>
#include<string.h>
main()
{
char str[50],ch,*ptr;
int i;
printf("enter a string\n");
scanf("%[^\n]s",str);
printf("enter a character\n");
scanf(" %c",&ch);
printf("strng address is at location:%u\n",str[0]);
for(i=0;str[i];i++)
{
if(str[i]==ch)
printf("char found at %d location\n",i);
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc array7.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter a string
welcome vector institute
enter a character
v
strng address is at location:119
char found at 8 location
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter a string
welcome
enter a character
e
strng address is at location:119
char found at 1 location
char found at 6 location