[below structure contains s1.name contain array of string name. to check if user entered name in that array list. i need to compare words by words. but my problem is that since string is stored in array. and string itself is an small array. so i dont know how can i reach to each single words of string stored in array.]


/*program to input students details in structure and display its out put and search availability of student by its name without using strcmp()*/
#include<stdio.h>
#include<string.h>

int main(){

int i,n;

struct school{
int regno;
char name[20];
int class;
}s1[10];


printf("\n enter the no of details of student to be added\n");
scanf("%d",&n); 

for(i=0;i<n;i++){
printf("enter the student %d details\n", i);

printf("enter reg no");
scanf("%d",&s1[i].regno);
printf("name ");
scanf("%s",&s1[i].name);
printf("class ");
scanf("%d",&s1[i].class);

}

printf("\n*****RESULT ARE AS FOLLOW*****\n");

printf(" NAME CLASS REG NO \n");
for(i=0;i<n;i++){
printf("%s %d %d ",s1[i].name,s1[i].class,s1[i].regno)
printf("\n");

}
}

Firstly, use fgets to obtain your name string from the user. See this for a reference (it's a C++ site but it should be nearly the same).

Next look into ctype.h and there's a function tolower() that will help you if you don't know whether the text is in caps or not (you could test whether or not it was for each character but we can just transform the whole thing to lowercase). If you can't use that library, you can write your own very quickly, just add the correct amount to the char value (see ASCII table)

You can access the character n (0 to size-1) of the string in the ith (0 to size-1) structure as follows:
s1.name[n] so use tolower() on each char
as you compare that to yourstring[n]
where char * yourstring= "yourstringtomatch";
(put any bells, whistles,code to skip spaces etc in there too)
march your loop until you hit the null terminator in the string '\0' count the number of times they match, compare that to the length of the string and you're in business.

Post back if there are any issues.

thanks buddy it work well ...i didnt know i can read each words by s1..name untill u helped me out. so thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.