Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6960855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:27:19+00:00 2026-05-27T15:27:19+00:00

What am I not getting here? This works pretty well: for ( i =

  • 0

What am I not getting here?

This works pretty well:

for ( i = 0; i < 5 && found != 0 ;++i ){ no difference than above
        found=strcmp( name, myContacts[i].cFirstName);
        printf(" i %d\n", i);
    }
printf(" \nName Found %s",  myContacts[i-1].cFirstName );

But just out of curiosity, I’m trying to use also strstr().

/*** This achieves the same functionality as above ***/

    for ( i = 0; i < 5  ;i++ ){
    found2=strstr( myContacts[i].cFirstName , name);
    printf(" i %d\n", i);
    if (found2 != NULL)
        {
        printf(" \nName Found %s",  myContacts[i].cFirstName );
        break;
        }
    }    

This however is not working:

for ( i = 0; i < 5 && found2 != '\0' ;i++ ){ //this does not work as above
for ( i = 0; i < 5 && found2 != NULL ;i++ ){ // this also is not wroking
        found2=strstr( myContacts[i].cFirstName , name);
}

printf(" \nName Found %s",  myContacts[i].cFirstName );

Thanks in advance for you suggestions.

Full code:

# please take my codes with a grain of skepticism, I am still learning 
# i know of sizeof(), but I rather not use it just for the purpose of my exercise

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define MAXBUFFERSIZE   10

typedef struct contact {

    char cFirstName[10];
    char cLastName[10];
    char cTelphone[12];

} address ; // end type

// function prototype
void printContacts( address * );
void printMenu();
char getChoice();
void storeContact( address [] ,  int *);
//int searchContact( address [] , char * );
void searchContact( address [] , char [] );

int main(){

    char cSelection = 0;
    address myContacts[5];
    char buffer[MAXBUFFERSIZE];

    int i ;
    // initialize array to be zeros
    for ( i = 0; i < 5 ; i++ ){
        strcpy(myContacts[i].cFirstName, "0");
        strcpy(myContacts[i].cLastName,"0");
        strcpy(myContacts[i].cTelphone,"0"); 
    }

    strcpy(myContacts[0].cFirstName, "Jonny");
    strcpy(myContacts[1].cFirstName, "Julia");
    strcpy(myContacts[2].cFirstName, "Claudia");
    strcpy(myContacts[3].cFirstName, "Aaron");
    strcpy(myContacts[4].cFirstName, "Sebastian");

    int iDel = -1 ; // store the position if one deleted
    int iCount = 0 ; // counter for position in the array, when 
                     // inserting names.

    while ( cSelection != '5' ) {
    printMenu();
    cSelection = getChoice();

    switch (cSelection) {
        case '1':
            printContacts( myContacts );    
            break;

        case '2':
            if ( ( iDel >= -1 ) && ( iCount < 5 ) ){
                //printf("\niCount is %d ", iCount);
                storeContact( myContacts, &iCount );
                iCount++;
                //printf("\nOutside storeContact, *Plocation %d", iCount );
                }
            if ( iCount >= 5 ) {
                printf("\nThe Memory is full, consider deleting some"\
                "Contacts");    
                }
            break;

        case '3':
            {            
            printf("\nEnter a name or part of a name to search:\n");
            fscanf(stdin, "%s", buffer);
            getchar(); // clear the last enter
            printf("\nThe line you entered was:\n");
            printf("%s\n", buffer);
            searchContact( myContacts, buffer );
            break;
            }       
        case '4':   
            //iDel=deleteContact( myContacts );
            break;

        }// end of switch
    }// end of while
    return 0;
} // end main

char getChoice(){

    char cSelection = 'q'; //for the menu
    /**** always scanf CHARS so you can check
     *    if digit or char !!! ****/

    scanf("%s", &cSelection);

    while ( strlen(&cSelection) != 1 ){
        printf("\nChoich not understood, enter a number again:");
        scanf("%s",&cSelection);
        }

    if ( isalpha(cSelection) ){
        printf( "You entered a letter of the alphabet\n" );
        cSelection = -1;
        printf( "Illegal choice !!!" );
      }

    return cSelection;
    } 

void printContacts( address * myContacts ){

    int i ;

    for ( i = 0; strcmp(myContacts[i].cFirstName,"0") != 0 && i < 5 ; i++ ){                                             
        printf("\nmyContacts[%d].cFirstName: %s", i, \
        myContacts[i].cFirstName );
    }// end for
}

void printMenu(){
    printf("\n\n\tSilly Phone Book\n\n");
    printf("\n\n1\tPrint Phone Book\n");
    printf("2\tAdd New Contact\n");
    printf("3\tSearch For Contact\n");
    printf("4\tDelete Contact\n");
    printf("5\tQuit\n");
    printf("\nSelect Action: ");
    }

//void storeContact( address myContacts[] ){ //syntactic sugar
void storeContact( address * myContacts,  int *Plocation ){ 

    char ch;                     /* handles user input */
    char buffer[MAXBUFFERSIZE];  /* sufficient to handle one line */
    int x = 0;
    x=*Plocation;

    ch = getchar(); // clear the last enter
    printf("Enter a name (<10 characters)\n");
    //ch = getchar();
    //char_count = 0;
    //while( (ch != '\n')  && (ch != EOF ) &&  (char_count < MAXBUFFERSIZE)) {
        //buffer[char_count++] = ch;
        //ch = getchar();
    //}
    //buffer[char_count] = 0x00;      /* null terminate buffer */

    //fgets(buffer,11,stdin);

    fscanf(stdin, "%s", buffer); /* read from keyboard */

    printf("\nThe line you entered was:\n");
    printf("%s\n", buffer);

    //TODO: add check that string is not too long!!!
    // if we do that, the code won't blow here ?

    strcpy(myContacts[x].cFirstName, buffer);    
} 

//int searchContact( address * myContacts,    char name[] ){
void searchContact( address * myContacts,    char * name ){
    int found;
    char *found2;
    //printf("\nHey dude im buffer from inside searchContact: %s", name);
    // iterate throught the array, print possible matches
    int i = 0;


    //for ( i = 0; i < 5 && found != 0 ;i++ ){
    //for ( i = 0; i < 5 && found != 0 ;++i ){ no difference than above
        //found=strcmp( name, myContacts[i].cFirstName);
        //printf(" i %d\n", i);
    //}
    //printf(" \nName Found %s",  myContacts[i-1].cFirstName );

    /*** This achieves the same functionality as above 

    for ( i = 0; i < 5  ;i++ ){
    found2=strstr( myContacts[i].cFirstName , name);
    printf(" i %d\n", i);
    if (found2 != NULL)
        {
        printf(" \nName Found %s",  myContacts[i].cFirstName );
        break;
        }
    }    ***/

    for ( i = 0; i < 5 && &found2 != '\0' ;i++ ){ //this does not work as above
        found2=strstr( myContacts[i].cFirstName , name);
        printf("found %p i %d\n", found2, i);
        //printf(" \nName Found %s",  myContacts[i].cFirstName );
    }

    //return found;
} // end of searchContacts  if ( isalpha(cSelection) ){
        printf( "You entered a letter of the alphabet\n" );
        cSelection = -1;
        printf( "Illegal choice !!!" );
      }

    return cSelection;
    } 

void printContacts( address * myContacts ){

    int i ;

    for ( i = 0; strcmp(myContacts[i].cFirstName,"0") != 0 && i < 5 ; i++ ){                                             
        printf("\nmyContacts[%d].cFirstName: %s", i, \
        myContacts[i].cFirstName );
    }// end for
}

void printMenu(){
    printf("\n\n\tSilly Phone Book\n\n");
    printf("\n\n1\tPrint Phone Book\n");
    printf("2\tAdd New Contact\n");
    printf("3\tSearch For Contact\n");
    printf("4\tDelete Contact\n");
    printf("5\tQuit\n");
    printf("\nSelect Action: ");
    }

//void storeContact( address myContacts[] ){ //syntactic sugar
void storeContact( address * myContacts,  int *Plocation ){ 

    char ch;                     /* handles user input */
    char buffer[MAXBUFFERSIZE];  /* sufficient to handle one line */
    int x = 0;
    x=*Plocation;

    ch = getchar(); // clear the last enter
    printf("Enter a name (<10 characters)\n");
    //ch = getchar();
    //char_count = 0;
    //while( (ch != '\n')  && (ch != EOF ) &&  (char_count < MAXBUFFERSIZE)) {
        //buffer[char_count++] = ch;
        //ch = getchar();
    //}
    //buffer[char_count] = 0x00;      /* null terminate buffer */

    //fgets(buffer,11,stdin);

    fscanf(stdin, "%s", buffer); /* read from keyboard */

    printf("\nThe line you entered was:\n");
    printf("%s\n", buffer);

    //TODO: add check that string is not too long!!!
    // if we do that, the code won't blow here ?

    strcpy(myContacts[x].cFirstName, buffer);    
} 

//int searchContact( address * myContacts,    char name[] ){
void searchContact( address * myContacts,    char * name ){
    int found;
    char *found2;
    //printf("\nHey dude im buffer from inside searchContact: %s", name);
    // iterate throught the array, print possible matches
    int i = 0;


    //for ( i = 0; i < 5 && found != 0 ;i++ ){
    //for ( i = 0; i < 5 && found != 0 ;++i ){ no difference than above
        //found=strcmp( name, myContacts[i].cFirstName);
        //printf(" i %d\n", i);
    //}
    //printf(" \nName Found %s",  myContacts[i-1].cFirstName );

    /*** This achieves the same functionality as above 

    for ( i = 0; i < 5  ;i++ ){
    found2=strstr( myContacts[i].cFirstName , name);
    printf(" i %d\n", i);
    if (found2 != NULL)
        {
        printf(" \nName Found %s",  myContacts[i].cFirstName );
        break;
        }
    }    ***/

    for ( i = 0; i < 5 && found2 != '\0' ;i++ ){ //this does not work as above
        found2=strstr( myContacts[i].cFirstName , name);
        printf("found %p i %d\n", found2, i);
        //printf(" \nName Found %s",  myContacts[i].cFirstName );
    }

    //return found;
} // end of searchContacts

Just for the completeness of this discussion, I’m adding what finally really worked as I wanted it.
This came after going through all the answers, so thanks everyone:

I forgot to initialize the pointer:

char *found2=NULL;

Now the following loop works as expected:

 for ( i = 0; i < 5 && !found2 ;i++ ){ //this does work as above

        found2=strstr( myContacts[i].cFirstName , name);
        printf("i %d\n", i);

    }
    printf("Name found %s", found2);

I wanted this functionallity with strstr() because now I can search “Clau” and match it to “Claudia”.
This is better for my needs than strcmp(), although I am quite sure it can be done also with strcmp(), with more sophistication or better skills in C than I have.

Thanks again for the answers!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:27:19+00:00Added an answer on May 27, 2026 at 3:27 pm

    Your found2 is a char *.

    You should write for (i = 0; i < 5 && !found2; i++) //etc etc

    (or test for found2 in the loop and break if non NULL)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This started over here when I was getting Class 'Classname_model' not found . I
I have been reading up on Cloud computing on here and still not getting
from here 48 struct snd_card *snd_cards[SNDRV_CARDS]; 49 EXPORT_SYMBOL(snd_cards); I am not getting whats the
I'm not getting the syntax right. Lets say I have this... #include <set> ...
I'm simply not getting this to work. I saw that this network isn't for
Have been getting pretty bald over this situation! I am using MS VS 2010
So, I'm getting a pretty strange error here... I tried to isolate it but
So I am pretty new to JQuery and just spent 5 hours getting this
I have a simple Spring & Jersey application, which works perfectly well for consuming
I don't understand this at all. Here is some Javascript code that works in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.