473,325 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

search for string from string array without using strcmp

2
following program does the display of student details but i want to search for the particular student name from the added list of student. so it would be great any one can help me without using strcmp() function



#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");

}
}
Dec 25 '09 #1
6 6463
weaknessforcats
9,208 Expert Mod 8TB
Is not using strcmp() a problem requirement?

If so, I would get this working by using strcmp(). Then I would replace the call to strcmp() with a call to my own function. Inside that function I would write code to compare the names.
Dec 25 '09 #2
cthoes
2
hai my problem was that since name are already stored as array of name in the structure school and can retrieve its array of name using only s1[i].name inside for loop. but my problem was that how can i compare if the user input name is available in the array of name s[i].name. becoz i was stucked since every string name in the array itself is one small array and to compare with user input name i need to compare with words by words . but i dont know how can i access to each words of string stored in array of string. so it would be great if you have any idea
Dec 26 '09 #3
APmore
14
so you have to compare two strings: string in record [s1] and user input string[s2]

Compare the lengths of these two strings;If they are unequal, they dont match.

If they are equal, compare the characters of these two strings one by one till you encounter '\0'.At any point of character comparision of strings, if they are not equal, these strings don't match.

Repeat this procedure for each of the strings in the record.

hope this helps
Dec 26 '09 #4
weaknessforcats
9,208 Expert Mod 8TB
Compare the lengths of these two strings;If they are unequal, they dont match.

If they are equal, compare the characters of these two strings one by one till you encounter '\0'.At any point of character comparision of strings, if they are not equal, these strings don't match.
Close. But you don't need to know the lengths of the strings. By the time you loop the strings to get the lengths, the strings could have been compared.

Here's the logic:

1) start at the first character of each string
2) compare the two characters
3) of the characters are equal, advance one character in each string and repeat step 2 and 3 until the characters are not equal
4) if one of the characters is a \0, the strings are equal
5) if character in the first string is > character in 2nd string, then first string is greater than second string
6) first string is less than second string
Dec 27 '09 #5
I didnt understand why can't we use strcmp() ?Can someone explain this properly.


Thanks
Dec 28 '09 #6
weaknessforcats
9,208 Expert Mod 8TB
I have no idea but the OP says in the title of the thread that strcmp() can't be used. Must be some kind of academic exercise.
Dec 28 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
4
by: sun6 | last post by:
this is a program counting words from "text_in.txt" file and writing them in "text_out.txt". it uses binary tree search, but there is an error when i use insert () thanks for any help ...
7
by: spike | last post by:
Im writing a program to search for a string in a binary file. And it works. The problem is: It is sooo slow! how can i make it faster? It takes 27 seconds just to search a 5 meg file. I guess it...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
31
by: lovecreatesbeauty | last post by:
Any comments are welcome for following the two sequential search C functions of both integer and string versions. Though they are simple, please do not laugh at it :) Could you give your great...
35
by: dtschoepe | last post by:
Greetings. I am working on an assignment and can't seem to get the right concept for something I'm attempting to do with enum data types. I have defined the following in my code: enum color...
3
by: lucky_therock | last post by:
hey..i ma newbie to c programming...its just my 3rd program..so i wanna know a code in which input string can be matched with strings in another opened file....!!
9
by: chutsu | last post by:
hi I got a simple program, and I was wondering how do you check if the string in an array = a string. For example if I put "APPLE" in array Array then how can I check it with a if statement. if...
1
by: 19831030 | last post by:
I wanna put string into array ,but I also wanna compare whole string using strcmp.So using 2 dim char array I can't do that,because if I use it I have to compare 2 strings by using char by char.But I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.