C program to accept a string using getchar( ), gets( ), scanf( ), scanf with editset command(^\n).

C program to accept a string using getchar( ), gets( ), scanf( ), scanf with editset command(^\n).

First we understand all the function of string which we use in our program, there are five function of string that is..

  • getchar( )
  •  gets( )
  •  scanf( )
  •  scanf with editset command(^\n).

getchar() : getchar basically use to take input from user, remember that getchar accept only single character, it unable to except string (if you want to except string using getchar it possible using loop but it is not best function to except string).

gets() : gets is use to take input from user, it is best function or method to except string, most of the case we use gets function while accepting (input) string from user.

scanf() :  scanf function is also use to take input from user, most of the case we are use scanf function instead of gets function but when we talk about string scanf function is not use (most of the case) because it unable to except space (“ ”) .

for example :

  • input is john kane ( remember input taken by scanf function)
  • output is john ( because scanf function unable to except space)

but there is one trick to except string using scanf function that is except string using [(^\n].

for example :

  • sancf(%[(^\n])    ( then scanf function accept space in string).

So now we write our program to accept a string using getchar( ), 

gets( ), scanf( ), scanf with editset command(^\n).

SOURCE CODE:

// write program to accept a string using getchar(),gets(),scanf(),scanf with endset commend(^\n)
#include<stdio.h>
#include<string.h>
void main(){
    int i=0,j;
    char name[19],name1[18],name2[25],name3[15];
    printf("\nEnter first name :");

      // string excepted by getchar:
    while((name[i] = getchar())!='\n'){
        i++;
    }
     // string excepted by scanf with endset commend(^\n)
     printf("enter second name:");
        scanf("%[^\n]",name3);

     // string excepted by gets:
     printf("enter third name:");
    gets(name1);
    gets(name1);

    // string excepted by scanf:
       printf("enter fourth name:");
        scanf("%s",&name2);
    printf("\n");

      printf("string excepted by getchar() function:\n");
    i=0;
    while(name[i]!='\0'){
        putchar(name[i]);
        i++;
    }

    printf("\n");

  printf("\nstring excepted by scanf with endset commend(^'\\n'):\n");
       printf("%s",name3);
    printf("\n");

  printf("\nstring excepted by gets() function:\n");
    puts(name1);
 
      printf("\n");
  
  printf("\nstring excepted by scanf function:\n");
       printf("%s",name2);

 

}
   
 OUTPUT:

Enter first name :roman
enter second name:john cena
enter third name:dean
enter fourth name:seth rollin

string excepted by getchar() function:
roman


string excepted by scanf with endset commend(^'\n'):
john cena

string excepted by gets() function:
dean


string excepted by scanf function:
seth
ANOTHER OUTPUT:
Enter first name :india
enter second name:china
enter third name:america A
enter fourth name:russia

string excepted by getchar() function:
india


string excepted by scanf with endset commend(^'\n'):
china

string excepted by gets() function:
america A


string excepted by scanf function:
russia

Above program except input by using all the function or method of string.







0 Comments