Saturday, August 15, 2026

Mobile Contact Manager || C Lab Programs

Program 12: Manually join names and check screen fit.

Problem Statement:: A basic mobile contact manager stores first and last names separately. For displaying full names in the contact list, the system needs to join them manually. Additionally, the system must check the length of each full name to ensure it fits within the 20-character screen limit.

Problem Description:

  • Input: First name (string) and Last name (string).

  • Output: Full name joined by a space, the total length, and a fit/no-fit message.

  • Constraints: Implement without using built-in string functions (like strcat or strlen); screen limit is 20 characters.

  • Method: Use character arrays and manual while loops to copy characters and calculate length.

Pgm Logic:

  1. Start.

  2. Input first name and last name strings.

  3. Initialize indices i = 0, j = 0, and length = 0.

  4. Loop through the first name array until the null terminator (\0) is reached, copying each character into the full name array and incrementing length.

  5. Insert a space character (' ') into full[length] and increment length.

  6. Loop through the last name array until \0, copying characters into full name and incrementing length.

  7. Terminate the full name string by adding \0 at the end.

  8. Print the full name and its calculated length.

  9. If length > 20, print "Name too long for screen"; otherwise, print "Name fits the screen".

  10. Stop.

Program Code:

// Purpose: To join first and last names manually and verify if they fit a 20-character screen.

#include <stdio.h>


int main()

{

    char first[10], last[10], full[20];

    int i = 0, j = 0, length = 0;


    printf("Enter first name: ");

    scanf("%s", first);

    printf("Enter last name: ");

    scanf("%s", last);


    // Manually copy first name into the full name array

    while(first[i] != '\0')

    {

        full[length++] = first[i++];

    }


    // Manually add a space between names

    full[length++] = ' ';


    // Manually copy last name into the full name array

    while(last[j] != '\0')

    {

        full[length++] = last[j++];

    }


    // Manually terminate the new string with a null character

    full[length] = '\0';


    printf("Full Name: %s\n", full);

    printf("Total Length: %d\n", length);


    if(length > 20)

    {

        printf("Name too long for screen.\n");

    }

    else

    {

        printf("Name fits the screen.\n");

    }


    return 0;

}


Output: 

Enter first name: Alexander 

Enter last name: Johnson 

Full Name: Alexander Johnson 

Total Length: 18 

Name fits the screen.

RESULT: Thus the program has been executed and the output was verified.

Remarks: This program was successfully compiled and tested in the onlineGDB tool. It bypasses string.h by directly interacting with the character indices of the arrays.

Program Explanation: Strings in C are essentially arrays of characters ending with a \0 (null) character. The program uses while loops to travel through the input arrays, stopping only when the \0 is detected. By manually moving characters to a third array (full), we perform concatenation. The final length check ensures the combined string doesn't exceed the hypothetical 20-character mobile display.


For all 2026 published C Lab Program posts Index page: click here

For all 2026 published articles list: click here

…till the next post, bye-bye & take care