Friday, November 23, 2018

C code review converts user input into upper or lower case string

This C code shows how to convert user input into upper/lower case string. And code is helpful in string manipulation task.

code:

//Tested Ok in Code::Blocks IDE 17.12.
//in Cygwin+GCC logic works, but Ctrl-Z makes problem

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

void lower_to_upper();
void upper_to_lower();
void main()
{
    int n;
top:
    system("cls"); //system("clear");
    printf("\nPlease enter your choice.");
    printf("\n\t(1) for upper to lower conversion.");
    printf("\n\t(2) for lower to upper conversion.");
    printf("\n\t(0) for exit.");
    printf("\nCHOICE:-  ");
    scanf("%d",&n);
    switch (n)
    {
    case 1:
    {
        printf("\nPlease enter a string in upper case.");
        printf("\nString will be terminated if you press EnterKey+Ctrl-Z.");
        printf("\nSTRING:-  ");
        upper_to_lower();
        break;
    }

    case 2:
    {
        printf("\nPlease enter a string in lower case.");
        printf("\nString will be terminated if you press EnterKey+Ctrl-Z.");
        printf("\nSTRING:-  ");
        lower_to_upper();
        break;
    }
    case 0:
        printf("\nHAVE A NICE DAY! BYE.");
        getch();
        exit(0);
        break;
    default:
        printf("\nERROR");
    }
  getch();
  goto top;
}
void upper_to_lower()
{
    int i,j;
    char c4[80],c3;
    for (i=0; (c3=getchar())!=EOF; i++)
        c4[i]=(c3>='A' && c3<='Z')?('a' + c3 -'A'):c3;
    printf("\nThe lower case equivalent is \n");
    for (j=0; j<i; j++)
        putchar(c4[j]);
    return;
}

void lower_to_upper()
{
    int i,j;
    char c2[80],c1;
    for (i=0; (c1=getchar())!=EOF; i++)
        c2[i]=(c1>='a' && c1<='z')?('A' + c1 -'a'):c1;
    printf("\nThe upper case equivalent is \n");
    for (j=0; j<i; j++)
        putchar(c2[j]);
    return;
}

//end of program

To see such C code reviews refer blog archieve at right side or click CCode lable for till posted posts list.

...till next post, bye-bye and take care.

No comments:

Post a Comment