Thread: Copying portions of arrays?

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    28

    Copying portions of arrays?

    I am currently taking a C class, and I have been assigned to


    1. Prompt the user and accept a string of no more than 254 characters
    2. Prompt the user and accept a second string of no more than 254 characters
    3. Create a third string consisting of the first half of string (1) and the second half of string (2). Display this string


    I know how to prompt for strings, but I'm having a little difficulty figuring out what tools I should use for this. I'm thinking that if I could get the length of the strings, I could divide the numbers in two, and I could use those numbers to create a loop and display the characters using < & >. However, I don't know how I would do this, or even if this is the correct method. Any help would be greatly appreciated.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You can use a loop until you find the '\0' character and increment your count variable to find out how many characters are in each string.

    ex)
    Code:
    for(stringCount=0; string[stringCount]!='\0' ;stringCount++)
    Last edited by camel-man; 11-07-2012 at 03:29 PM.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    28
    Thank you! It helped, and I was able to pull it off. However, I have a new issue. I'm trying to get a character count, and I'm having an issue with any showing up. Perhaps you could help me with what I'm messing up on:
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    
    
    
    
    
    
    int main()
    {
        char str1[255], str2[255], combinedStr [255], newSentence [101];
        int lenOfStr1, lenOfStr2, lenOfNewSent, newSentenceArray [128]={0}, totalArray [128]={0};
        char currentChar, currentChar2;
        int i = 0, totLtrs = 0;
        double percentage = 0.00;
    
    
        printf("Please enter a string of 254 characters or less:");
        fgets(str1,254,stdin);
        printf("Please enter a second string of 254 characters or less:");
        fgets(str2,254, stdin);
        lenOfStr1 = strlen (str1);
        lenOfStr2 = strlen (str2);
        str1[lenOfStr1 - 1] = '\0';
        str2[lenOfStr2 - 1] = '\0';
        printf("Your first  string is %d characters long.\n", lenOfStr1);
        printf("Your second string is %d characters long.\n", lenOfStr2);
        strncpy(combinedStr, str1, lenOfStr1 / 2);
        combinedStr[lenOfStr1 / 2] = '\0';
        strcat(combinedStr, str2 + lenOfStr2 / 2);
        printf("String 3 is %s\n", combinedStr);
        printf("Now, please enter a sentence of up to 100 characters:");
        fgets(newSentence, 100, stdin);
    
    
        lenOfNewSent = strlen (newSentence);
        newSentence[lenOfNewSent - 1] = '\0';
    
    
        while (newSentence[i] != '\0')
        {
            currentChar = newSentence[i];
            newSentenceArray[currentChar]++;
            ++i;
        }
    
    
        totLtrs = i;
    
    
        printf("\nFor the string you just wrote\n\n\n", i);
        printf("FREQUENCY TABLE\n-------------------\nChar   Count   %% of Total\n");
        printf("----   -----   ----------\nALL\t%d\t100.00%%\n", totLtrs, percentage);
    
    
        for (i=0; i < 128; ++i)
        {
            if (newSentenceArray[i] > 0)
            {
                printf("'%c'\t%d\t%-2.2f%%\n", i, newSentenceArray[i], percentage = ((double) newSentenceArray[i]/(double) totLtrs) * 100.00);
            }
        }
    
    
        strcpy (combinedStr, str1);
        strcat (combinedStr, str2);
        strcat (combinedStr, newSentence);
        lenOfNewSent = strlen (combinedStr);
        combinedStr[lenOfNewSent - 1] = '\0';
        i=0;
    
    
        printf("The new combinesStr is %s", combinedStr);
    
    
    
    
        while (combinedStr[i] != '\0')
        {
            currentChar2 = totalArray[i];
            totalArray[currentChar2]++;
            ++i;
        }
    
    
        totLtrs = i;
        printf("i is %d", i);
    
    
        printf("\nFor everything you've written\n\n\n");
        printf("FREQUENCY TABLE\n-------------------\nChar   Count   %% of Total\n");
        printf("----   -----   ----------\nALL\t%d\t100.00%%\n", totLtrs);
    
    
        for (i=0; i < 128; ++i)
        {
            if (totalArray[i] > 0)
            {
                printf("'%c'\t%d\t%-2.2f%%\n", i, totalArray[i], percentage = ((double) totalArray[i]/(double) totLtrs) * 100.00);
            }
        }
    
    
    
    
    
    
    
    
    
    
        return 0;
    }
    The first loop works perfectly, but I can't get anything with the second. Any ideas?

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Check line 80. You're probably using the wrong array.

    Bye, Andreas

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    28
    Quote Originally Posted by AndiPersti View Post
    Check line 80. You're probably using the wrong array.

    Bye, Andreas
    What? No, I checked that out a dozen times last ni.... ohhhhHHHHHHHHHHHHHHH ARRGGGHHHHH!!!!!!

    HOW COULD I HAVE MISSED THAT??!!!!!

    *clears throat*

    Thank you, that was quite helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying arrays...
    By darren78 in forum C++ Programming
    Replies: 7
    Last Post: 08-20-2010, 10:55 AM
  2. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  3. needs help copying arrays
    By RedRattler in forum C Programming
    Replies: 3
    Last Post: 04-04-2003, 11:34 PM
  4. Copying Arrays
    By conright in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 04:09 PM
  5. help on copying arrays
    By neversell in forum C Programming
    Replies: 3
    Last Post: 06-30-2002, 04:22 PM