Thread: More than 8 strings

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    22

    Question More than 8 strings

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAXLINES 8
    
    int main()
    {
    	char *lines[MAXLINES];
    	int n=0; 
    	char buffer[80];
    
    	puts("Enter one line at time enter a blank when done.");
    
    	while((n<MAXLINES) && (gets(buffer) !=0) && (buffer[0] !='\0'))
    	{
    		if((lines[n]=(char*)malloc(strlen(buffer)+1)) ==NULL)
    			return -1;
    
    		strcpy(lines[n++],buffer);
    	}
    
     return 0;
    }
    Question:
    Over here, there are 8 arrays to store 8 independent strings. The length of each strings is unlimited as long as the memory is available, so, there is flexibility i.e. no wastage of space.
    But that’s only for the length of the string. I found out that by declaring 8 arrays, the program is no longer flexible. The user cannot enter more than 8 independent strings and if the user enter less than 8, memory is wasted. How do I implement the flexibility as in the length of those strings?

  2. #2
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531
    How about link list?
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Memory is cheap. Even an array of 1000 char*'s is a mere 4K. So don't be so bashful. Also, you should really switch to fgets(), because if the user enters more than 80 characters in your program, it will certainly crash!

    A linked list is the only way to go if you want a real dynamic structure, though. Just a suggestion.


    Code:
    //.............................
    
    
    
    int IsEmpty(char buff[] )
     {
       int x, len = strlen(buff);
    
       for( x = 0; x < len; x++)
        {
          if( !isspace(buff[x]) )  return 1;  
        }
    
     return 0;
     }
    
    
    //.............................
    
    
    
    
    #define MAXLINES 1000
    
    int main()
    {
     char *lines[MAXLINES];
     int n=0; 
     char buffer[80];
    
     puts("Enter one line at time enter a blank when done.");
    
     while( n < MAXLINES ) 
      {
        fgets(buffer, 80, stdin); //...This function prevents overflow!!
    
        if( IsEmpty( buffer ) ) break;
    
        if( (lines[n]=(char*)malloc(strlen(buffer)+1) ) ==NULL)  return -1;
    
        strcpy(lines[n++],buffer);  
      }
    
     return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    or use realloc
    a quick and dirty solution
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAXBUF 1024
    
    int
    add_str(char ***ppstr,int strpos)
    {
    	char buf[MAXBUF];
    	char **tmppstr;
    	int l;
    	tmppstr=realloc(*ppstr,(strpos+1)*sizeof(**ppstr));
        if(tmppstr==NULL)
    	{
    		perror(NULL);
    		return 1;
    	}
    	fgets(buf,MAXBUF,stdin);
    	/*
    	remove '\n' from buf if you wish here
    	*/
    	if(ferror(stdin))
    	{
    		perror(NULL);
    		return 1;
    	}
    	if( (tmppstr[strpos]=malloc(strlen(buf)+1))==NULL )
    	{
    		perror(NULL);
    		return 1;
    	}
    	strcpy(tmppstr[strpos],buf);
    	*ppstr=tmppstr;
    	return 0;
    }
    
    
    
    int
    main(void)
    {
    	char **pstr=NULL;
    	int strpos=0,i;
    	for(i=0;i<5;i++)
    	{
    		add_str(&pstr,i);
        }
    	for(i=0;i<5;++i)
    		printf("%s",pstr[i]);
    	/*
    	    to write a function to free memory
    		free_str(&pstr,5);
    	 */
    
    	return 0;
    }
    The one who says it cannot be done should never interrupt the one who is doing it.

  5. #5
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    >The length of each strings is unlimited as long as the memory is available, so, there is flexibility i.e. no wastage of space.
    False. With char buffer[80];, you have a maximum of 80 characters that you can read in each line, and if the user entered more, there are always possibilities that your program would crash. And if the user entered less, what about the extra space in each line?
    Try to avoid, gets(), implement fgest() instead, a reliable counterpart.

    >How do I implement the flexibility as in the length of those strings?
    Data Structures is your solution (Linked List, Binary Tree, ...)

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Well, compliments of myself and quazah's valuable insight,
    here's a solution:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAXLINES 5
    
    int AddString(char*** a_string, const char* text, int* size);
    
    int main()
    {
    	char **lines=NULL;
    	int n=0, i; 
    	char buffer[100];
    
    	puts("Enter one line at time enter a blank when done.");
    
    	while((n!=MAXLINES) && (fgets(buffer,99,stdin) != NULL) && (buffer[0] !='\0') )
    		AddString(&lines,buffer,&n);
    		
    	for(i=0;i<n;i++)
    	{
    		printf("%s",lines[i]);
    		free(lines[i]);
    	}
        
                    
        free(lines);
        return 0;
    }
    
    
    
    int AddString(char*** a_string, const char* text, int* size)
    {
    	int i=0,j=0;
    	char** newlist;	
    	
    	if( (newlist = malloc(sizeof(char*)*(*size+1)))==NULL);
    	
    	for(i=0; i < *size;i++)
        	newlist[i] = (*a_string)[i];
    	
    	free(*a_string);
    	
    	if( (newlist[i] = malloc(strlen(text)+1)) == NULL )return 1;
    	memcpy(newlist[i],text,strlen(text)+1);
        *a_string = newlist;
    	*size += 1;
        return 0;
    }

  7. #7
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    One thing that i hadn't bothered to check was that you should
    change this,
    Code:
    while((n!=MAXLINES) && (fgets(buffer,99,stdin) != NULL) && (buffer[0] !='\0') )
    
    //with this
    
    while( (fgets(buffer,99,stdin) != NULL) && (buffer[0] !='\n') )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM