Thread: strings & arrays question

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    9

    Question strings & arrays question

    Code:
    #include <stdio.h>
    
    void readLine (char buffer[])  //function to read what I type in the terminal
    {
        int i = 0, character;
        
        do
        {
            character = getchar ();
            buffer[i] = character;
            ++i;
        }
        while (character != '\n');
        
        buffer[i-1] = '\0';
    }
        
    int lenghtString (char source[]) // function to return the lenght of the string
    {
        int count = 0;
        
        while (source[count] != '\0')
            
            ++count;
        
        return count;
    }
    
    void insertString (char source[], char insert[], int position) //Function to insert a string inside another string from a given position
    {
        int i, j, charKeep, lenght;
        int lenghtString (char source[]);
        
        lenght = lenghtString (source) + lenghtString (insert);
        
        for (i = position, j = 0; i < lenght;)
        {
       
             if (insert[j] != '\0')
             {
                charKeep = source[i];
                source[i] = insert[j];
                insert[j] = charKeep;
                ++j;
                ++i;
             }
             else
                 j = 0;
        }
             
        source[lenght] = '\0';
    }
    
    void showString (char source[]) //Function to show a string
    {
        int i;
        
        for (i = 0; source[i] != '\0'; ++i)
        
            printf ("%c", source[i]);
        
        printf ("\n");
    }
    
    int main (void)
    {
        char source[81], insert[81];
        void insertString (char source[], char insert[], int position), readLine (char buffer[]), showString (char source[]);
        int position;
        
        printf ("Ingrese la/s palabra/s:\n"); //type in the string
        readLine (source);
        
        printf ("Ingrese la palabra a insertar:\n"); //type in the string to be inserted
        readLine (insert);
        
        printf ("Ingrese la posicion en la cual desea insertarla: "); //type the position from where the previous string will be inserted
        scanf ("%i", &position);
        
        insertString (source, insert, position);
    
        showString (source);
        
        return 0;
    }
    This program is to insert a string inside another from a given position.
    My question is why the function insertString works fine. I thought that an array, even if I set 81 to be the size, will "compress" somehow to the lenght of the string I type in the terminal and the new size is (less than 81) cant be expanded anymore. (pd: sorry for my english. If something is not clear let me know)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Griffith
    My question is why the function insertString works fine. I thought that an array, even if I set 81 to be the size, will "compress" somehow to the lenght of the string I type in the terminal and the new size is (less than 81) cant be expanded anymore
    The size of the array and the length of the string are two different things. When we talk about a string, we mean a contiguous sequence of characters terminated by a null character. This can be stored in an array that has just enough space for it, or it could be stored in a much larger array, in which case the remainder of the array is left unused, hence it could be used later to lengthen the string. It is even possible to store multiple strings in a single array, which is basically what happens when you use strtok.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays of strings
    By mbooka in forum C Programming
    Replies: 2
    Last Post: 02-19-2006, 07:55 PM
  2. Beginners question about arrays/strings
    By Wiretron in forum C Programming
    Replies: 2
    Last Post: 11-07-2005, 03:34 PM
  3. question about strings, and one about arrays.
    By voodoomagic in forum C Programming
    Replies: 2
    Last Post: 10-25-2004, 04:23 PM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. question about arrays of strings
    By lakai02 in forum C Programming
    Replies: 14
    Last Post: 12-25-2002, 09:11 PM

Tags for this Thread