Print Commands


General Statement: Read a string and output the results of the commands FIRST, BUTFIRST, LAST, and BUT LAST.

Input: The first line of the data set for this problem is an integer that represents the number of strings that follow. Each string is on a separate line.

Output: Output is to be in 2 columns. The first column is the commands in order FIRST, BUT FIRST, LAST, BUT LAST. The second column in the result of the command. Each column is to be left-aligned with some white space between columns.
The output is to be formatted exactly like that for the sample output given below.

Assumptions: All letters are upper case.
The string contains only letters and 1 space between words.
The maximum string length is 80.
The maximum word length is 12.
The maximum number of words is 6.

Discussion:

Command

Word

Sentence

FIRST

1st letter

1st word

BUT FIRST

Omit 1st letter

Omit 1st word

LAST

Last letter

Last word

BUT LAST

Omit last letter

Omit last word

Sample Input:
3
VISIT US ON THE WEB
ADDRESS
EAGLE PASS IS IN TEXAS

Sample Output:
FIRST VISIT
BUT FIRST US ON THE WEB
LAST WEB
BUT LAST VISIT US ON THE

FIRST A
BUT FIRST DDRESS
LAST S
BUT LAST ADDRES

FIRST EAGLE
BUT FIRST PASS IS IN TEXAS
LAST TEXAS
BUT LAST EAGLE PASS IS IN

Solutions :

#include <stdio.h>

int main ()
{
    int dataset;
    scanf ("%d", &dataset);
    getchar ();

    while ( dataset-- ) {
        char str [82];
        gets (str);

        bool space = false;

        for ( int i = 0; str [i] != 0; i++ ) {
            if ( str [i] == ' ' ) {
                space = true;
                break;
            }
        }

        if ( space ) { // sentence
            int i = 0;
            printf ("FIRST           ");
            for ( i = 0; str [i] != ' '; i++ )
                printf ("%c", str [i]);

            printf ("\nBUT FIRST      ");
            while ( str [i] != 0 )
                printf ("%c", str [i++]);

            printf ("\nLAST            ");
            int last_space;
            for ( i = 0; str [i] != 0; i++ ) {
                if ( str [i] == ' ' )
                    last_space = i;
            }

            for ( i = last_space + 1; str [i] != 0; i++ )
                printf ("%c", str [i]);

            printf ("\nBUT LAST        ");
            for ( i = 0; i < last_space; i++ )
                printf ("%c", str [i]);

            printf ("\n\n");

        }

        else { // word
            int i = 0;
            printf ("FIRST           %c", str [0]);

            printf ("\nBUT FIRST       ");
            for ( i = 1; str [i] != 0; i++ )
                printf ("%c", str [i]);

            printf ("\nLAST            %c", str [i - 1] );

            printf ("\nBUT LAST        ");
            for ( i = 0; str [i + 1] != 0; i++ )
                printf ("%c", str [i]);

            printf ("\n\n");
        }
    }

    return 0;
}

One thought on “Print Commands

  1. #include
    #include
    int main(){
    char sen[80];
    int x,a,b,c,d,e,f,g,z;
    printf(“please enter the number of inputs: “);
    scanf(“%d”,&x);
    printf(“\n”);
    fgets(sen,80,stdin);
    for(z=1;z<=x;z++){
    c=0;
    printf("please enter a sentence or word: ");
    fgets(sen,80,stdin);
    printf("\n");
    b=strlen(sen)-1;
    for(a=0;a<80;a++){if(sen[a]=='\n'){sen[a]='\0';}}
    for(a=0;a<b+1;a++){if(sen[a]==' '){c=c+1;}}
    printf("c = %d\n",c);
    printf("\n");
    if(c==0){
    printf("FIRST %c",sen[0]);
    printf("\n");
    printf("\nBUT FIRST ");
    for(a=1;a<b+1;a++){
    printf("%c",sen[a]);}
    printf("\n");
    b=strlen(sen)-1;
    printf("\nLAST %c",sen[b]);
    printf("\n");
    printf("\nBUT LAST ");
    for(a=0;a<b;a++){
    printf("%c",sen[a]);}
    printf("\n");}
    else{
    b=strlen(sen)-1;
    for(d=0;d<=b;d++){
    if(sen[d]==' '){e=d;break;}}
    printf("FIRST ");
    for(a=0;a<e;a++){printf("%c",sen[a]);}
    printf("\n");
    printf("\nBUT FIRST ");
    for(f=e+1;f<b+1;f++){printf("%c",sen[f]);}
    for(d=0;d<=b;d++){
    if(sen[d]==' '){g=d;}}
    printf("\n");
    printf("\nLAST ");
    for(f=g+1;f<b+1;f++){printf("%c",sen[f]);}
    printf("\n");
    printf("\nBUT LAST ");
    for(f=0;f<g;f++){printf("%c",sen[f]);}
    printf("\n");
    }
    }
    return 0;
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s