Katie Melosto
Katie Melosto

Reputation: 1177

how to manipulate char array in C

I am practicing learning C and I creating program where user inputs an int and then inputs multiple first and last names. The program will then print all the names last name first name. I tried to find this question in previous posts such as this, but could not find a good answer to my issue. For example,

INPUT:

2

Jack Jones

Myla Goo

OUTPUT:

Jones Jack

Goo Myla

Instead, my program prints out jargon:

���4LJo �5Lne Jack

Go Myla

Below is my code:

#include <stdio.h>
#include <string.h>
int main(void) {
    //! word1 = showArray(word1, cursors=[i], width=0.5)
    //! word2 = showArray(word2, cursors=[i], width=0.5)
    int num;
    //char word2[8];
    int count = 0;
    char first[101];
    char last[101];
    char all[1000];
    int last_letter_of_all;
    
    scanf("%i", &num);
    while(count != num){
        
        scanf("%s %s", first, last);
        int len_first = strlen(first);
        int len_last = strlen(last);
        //printf("%i %i\n", len_first, len_last);
        
        //add letters of last
        for(int k = 0; k < len_last; k++)
        {
            last_letter_of_all = strlen(all);
            char c = last[k];
            all[last_letter_of_all] = c;
           // printf("%c\n", c);
        }
        
        //find last location of last name added
        all[last_letter_of_all] = ' ';
        
        //add first name 
        for(int l = 0; l < len_first; l++)
        {
            last_letter_of_all = strlen(all);
            char c = first[l];
            all[last_letter_of_all] = c;
        }
       
        
        //find last space of location
        all[last_letter_of_all+1] = '.';
        count++;
    }
    
    for(int i = 0; i < strlen(all); i++)
    {
        if(all[i] != '.')
        {
         printf("%c", all[i]);
        }
        else
        {
            printf("\n");
        }
    }
   
    return 0;
}

I've been struggling with this for awhile. I come from Java so unfamiliar with C but I believe this may have something to do with the '/0' that signals the end of the array.

Upvotes: 0

Views: 146

Answers (1)

lost_in_the_source
lost_in_the_source

Reputation: 11237

You should use a struct to represent a single firstname-lastname pair. Then you have an array of these structures to hold the names. You allocate the array using malloc and not regular array syntax because you don't know how big the array will be. Then you populate the entries of the array.

After the array is populated, you walk through the array this time, using printf to print each entry, but last name first and first name last.

(error-checking omitted)

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

typedef struct {
    char first[101], last[101];
} name;

int main()
{
    int num;
    name *arr;
    int i;

    puts("How many names?");
    scanf("%d", &num);

    arr = malloc(num * sizeof(name));

    /* populate entries of the array */
    puts("Enter [first last] names");
    for (i = 0; i < num; ++i)
        scanf("%100s %100s", arr[i].first, arr[i].last);

    /* print each entry, but with last name first and first name last */
    for (i = 0; i < num; ++i)
        printf("%s %s\n", arr[i].last, arr[i].first);

    free(arr);
    return 0;
}

As a side note, it is always a good idea to include the maximum width of the buffer in %s for scanf.

Upvotes: 2

Related Questions