2 Replies - 218 Views - Last Post: 11 February 2014 - 06:44 AM Rate Topic: -----

#1 damatuse  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-February 14

Using strings to find out if a name is repeated

Posted 10 February 2014 - 11:10 PM

I have to write a code that will read in n amount of names and the names. Then it will tell me if the first name entered has been repeated or not.
This is my input:
5 Bush Reagan Lincoln Bush Obama

This is my output:
First name in list is not repeated.
First name in list is not repeated.
First name in list is repeated.
First name in list is not repeated.

I would like to just give one answer; if it is repeated then the output should simply be "First name in list is repeated."
If the answer is that it was not repeated, I would like the output to say once "First name in list is not repeated."

Not sure what I am doing wrong but if someone would help it would be greatly appreciated.

// Charles Matusevich
// COP3223
// Secion 2
// lastnames.c
// 02/09/14

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

int main(void) {

    char FIRST[25];
    char NAMES[25];
    int i, N, YESREPEAT, NOREPEAT;
    int freq = 1;

    printf("Enter n, followed by n Last Names (each last name must be a single word):\n");
    scanf("%d", &N);
    scanf("%s", FIRST);

    for (i=0;i<N;i++) {

        scanf("%s", NAMES);

        if (strcmp(NAMES, FIRST) == 0)
            printf("First name in list is repeated.\n");
        else
            printf("First name in list is not repeated.\n");

    }

    system("pause");
    return 0;

}



Is This A Good Question/Topic? 0
  • +

Replies To: Using strings to find out if a name is repeated

#2 Black_Epiphany  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 66
  • Joined: 02-February 14

Re: Using strings to find out if a name is repeated

Posted 11 February 2014 - 01:31 AM

I'm not going to write the code for you because it's simple but I'll give you some advice:

  • Look at what is within your for loop and maybe consider using a different type of loop (actually a for loop is just fine for this purpose, it's up to you)
  • Instead of just printing "First name in list is repeated", how about you have a variable that keeps track of whether or not it is repeated (0 for false 1 for true or something of the sort)
  • You're going to want to print "First name is repeated" or "First name is not repeated" after your loop, not within the loop

This post has been edited by Black_Epiphany: 11 February 2014 - 01:32 AM

Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 4292
  • View blog
  • Posts: 13,458
  • Joined: 25-December 09

Re: Using strings to find out if a name is repeated

Posted 11 February 2014 - 06:44 AM

One other thing, never, Never, NEVER use a function that doesn't limit the number of characters it will retrieve into a C-string. Without the optional width specifier scanf() can retrieve more characters than the size of the destination C-string which can lead to buffer overflow.

    char FIRST[25];
 
    scanf("24%s", FIRST); // Don't forget the width specifier!!


Also remember two other things about scanf(), first the width specifier must leave room for the end of string character. Note in the above how the size of the array is 25 and the width specifier is 24. Second remember that scanf() stops processing characters when it encounters a whitespace character.

Because of these two "gotchas" you may want to consider using fgets() instead. With fgets() you use the size of the array for the character limit, and it will retrieve all characters until it either reaches the character limit - 1 or it encounters the end of line character. This function automatically adds the end of string character to the string so it makes it easier to use #defined constants for the size of the array instead of the magic numbers like 25.

// In the global scope.
#define MAX_SIZE 25

// In main().
char first[MAX_SIZE];

fgets(first, MAX_SIZE, stdin); // Retrieve the first name from the console.



The only "gotcha" about fgets() is that it also places the end of line character in the string, in most cases. So if you don't what this character in the string you'll need to remove it yourself.

Also a convention in C/C++ is that all upper case is reserved for macros.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1