search for string from string array without using strcmp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cthoes
    New Member
    • Dec 2009
    • 2

    search for string from string array without using strcmp

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

    }
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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.

    Comment

    • cthoes
      New Member
      • Dec 2009
      • 2

      #3
      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

      Comment

      • APmore
        New Member
        • Jul 2007
        • 14

        #4
        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

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by ApMore
          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

          Comment

          • puneetsardana88
            New Member
            • Aug 2009
            • 57

            #6
            I didnt understand why can't we use strcmp() ?Can someone explain this properly.


            Thanks

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              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.

              Comment

              Working...