Thread: confused by strings

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    2

    Post confused by strings

    Im trying to write a program that reads in strings and decides if the 1st one is repeated. I cant figure out how to store the first string into a variable, and compare that variable to the rest of the inputted strings.

    ANY help or ideas would be freakin cool.


    Code:
       #include <strings.h>
       #include <stdio.h> 
      
       int main () { 
           
    //Declared variables     
           int i;
           int n;
           char curname[250];
           char lname[250]; 
           int freq;
           freq=1;
      
    //Scans in n for n strings      
         printf("Enter n, followed by n Last names (each last name bust be a single word):\n");
         scanf("%d",&n);
    
    
    //Loops for n strings    
         for (i=1; i<=n; i++){
    
    
    //Scans in strings 
         scanf("%s", curname);
    
    
    //Stores first string in curname 
      if (i==1) {
      strcpy(lname,curname); }
      
    //checks for repeated names 
         if  (strcmp(curname, lname) == 0) {
          freq++; 
          }
          }
          if (freq > 1) {
          
       
       printf("First name in list is repeated.\n"); }
             
       else if (freq == 1) {
            
       printf("First name in list is not repeated.\n");}
      
      
    
    
      system("PAUSE");
      return 0;
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    What you need is an array of strings, rather than an array of characters.

    Code:
    char* [10]
    This can store up to ten strings. Careful for memory management.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    2
    alright so i made a 2d array that holds 1000 strings. how would i store each incoming name into that array? strcmp ((names[0],name) == 0) didnt work

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    strcpy is used for copying strings.

    strcmp is used to compare strings.

    But, what you need first to look into is how to allocate memory with malloc.

    However, despite this is what will allocate the less memory needed for you, I suggest you to use a 2D array with fixed sizes.

    Here is an example. Read the comments and then try to modify your code

    Code:
    #include <stdio.h>
     
    int main(void){
       
        char stringsArray[10][30];
        /* What the dimensions mean? */
        /* The first dimension is how many strings */
        /* we are going to allocate. */
        /* The second allocation states the max length of */
        /* every string. */
        /* So, we have an array which can store ten strings */
        /* of max length 30-1 letters. Why -1? For the null terminator. */
        
        int i;
        printf("Input 10 strings\n");
        for(i = 0 ; i < 10 ; i++)
        {
            /* 29 stands for the maximum length */
            scanf("%29s", stringsArray[i]);
        }
        
        /* Do work */
        
        printf("Printing\n");
        for(i = 0 ; i < 10 ; i++)
        {
            printf("%s\n", stringsArray[i]);
        }
        
        return 0;
     
    }
    We used scanf. Hope that helps. Also welcome to the forum
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-16-2012, 06:08 AM
  2. Getting confused with strings and length
    By FreeCare in forum C Programming
    Replies: 15
    Last Post: 10-24-2008, 02:02 PM
  3. I'm getting confused on the strings...
    By Rune Hunter in forum C++ Programming
    Replies: 7
    Last Post: 08-22-2004, 10:13 AM
  4. array of strings confused me
    By linucksrox in forum C Programming
    Replies: 6
    Last Post: 05-08-2004, 09:10 PM
  5. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 03:13 AM

Tags for this Thread