chanzerre
chanzerre

Reputation: 2429

C- Character strings and loops

I have the following question:-

Write a program that takes a first name and last name entered by the user and displays last name, a comma and first initial,followed by a period:

The user's input may contain extra spaces before the first name, between the first and last names, and after the last name.

And I wrote the code for it like this:

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

int main(void)
{
    int i=0,j=0;
    char name[100];
    gets(name);

    while( name[i] == ' ' || name[i] == '\t' )
        i++;

    while( *(name+i) != ' ' && *(name+i)!= '\t' )
        i++;

    while(name[i] == ' '|| name[i] == '\t')
        i++;

    while( *(name+i) != ' ' && *(name+i) != '\t' && *(name+i) != '\0' )
        putchar(name[i++]);
        putchar(',');

    while( name[j] == ' '|| name[j] == '\t' )
        j++;

    while( *(name+j) != ' ' && *(name+j) != '\t' )
      {
        putchar(name[j++]);
        break;
      }
        putchar('.');
        return 0;
}

Though it is working but it seems it is somehow unacceptable.How can I improve upon it?

Upvotes: 1

Views: 325

Answers (2)

AndersK
AndersK

Reputation: 36102

you can compress it a bit by using sscanf:

#define MAX_LENGTH 100
...
char name[MAX_LENGTH];
char surname[MAX_LENGTH];
char firstname[MAX_LENGTH];
fgets( name, MAX_LENGTH, stdin ); 
if ( sscanf( name, "%s %s", firstname, surname ) == 2 )
{
  printf( "%s, %c.\n", surname, firstname[0] );
}

Upvotes: 1

Gangadhar
Gangadhar

Reputation: 10516

Avoid using gets() and use fgets().

From the man pages:

BUGS

Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.

EDIT

You can minimize your code by using isalpha,isspace functions from ctype.h.

isspace() : checks for white-space characters. In the "C" and "POSIX" locales, these are: space, form-feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal tab ('\t'), and vertical tab ('\v').

isalpha() : checks for an alphabetic character; in the standard "C" locale, it is equivalent to (isupper(c) || islower(c)). In some locales, there may be additional characters for which isalpha() is true letters which are neither upper case nor lower case.

you can use this refactored code or else any part of code.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_LENGTH 100


int main(void)
{
   int i=0,j=0;
   char name[MAX_LENGTH];

   fgets(name,MAX_LENGTH,stdin); //instead of gets(name); 

   for(i=0;i<strlen(name) ;i++)   //loop to capture initial ,after this you have initial name[i]
         if(isalpha(name[i]))
                break;

   for(j=i;j<strlen(name);j++)    //loop to find  start of  last name
        if(isspace(name[j]))
             if(isalpha(name[j+1]))
                     break;

    for(j=j+1;j<strlen(name);j++) //loop to print last name on screen
             if(isalpha(name[j]))
                  putchar(name[j]);
             else
                  break;

         putchar(',');                
         putchar(name[i]);           //print initial
         putchar('.');

         printf("\n");
  return 0;

 }

Upvotes: 2

Related Questions