Showing posts with label c program example. Show all posts
Showing posts with label c program example. Show all posts

Friday, October 16, 2020

, ,

C program for ferror() function

 ferror() function in C Programming language is used to identify the error in given c file. 

C program for ferror() function
C program for ferror()

 

In this tutorial you will learn about how to use the ferror() function and programing syntax.

 Prototype :-  

 int ferror(FILE *filename);

Parameters :-

FILE *filename

C program for ferror() function

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

int main()
{
	FILE *fr;
	char str[100];

	if((f=fopen("help.txt","r"))==NULL){
		printf("Cannot open the file...");
		//file is not exist program is terminated
		exit(1);
	}

	// Check if here is some error in the file
	if(ferror(f))
		printf("print the Error to read the file\n");
	else	
		printf("No error\n");

	printf("File content is--\n");
	//print the strings until EOF is encountered
	while(!feof(fr)){
		fgets(str,100,fr);
		//print the string
		printf("%s",str);
	}

	//remember to close the opened file
	fclose(f);

	return 0;
}

Wednesday, October 14, 2020

,

System() function program in c

 In System library function in c use the command int system(const char *command) name or program name to the host environment and also executed command processor and returns the command after complete the task that is given in the c program.

 Parameters For System() function program in c :

Command:-  This string in c program containing the name of requested variable.  
 
Return the status of the command at the success and  returned is -1 on error.
 
Example:-

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> int main () { char command[50]; strcpy( command, "-l" ); system(command); return(0); }


Example:-

This function list down the dir file with the help of c program

#include <stdio.h>
#include <string.h>

int main () {
   char command[50];

   strcpy( command, "dir" );
   system(command);

   return(0);
} 


system program in c
system program in c

Sunday, October 11, 2020

, ,

Example Program For Lab() Function In C Programming

 Lab() Function in c programming gives the absolute value of the integer and it return the absolute value of the long integer argument.

Showing program will tell how you can use the LAB function in c programming.

Example Program For  Lab() Function In C Programming
Example Program For  Lab() Function In C Programming

 

Parameters:-

Y − is a integral value. 

Return Value:-

This lab function returns the absolute value of y. 

LAB Function Program Example :-

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

int main () {
   long int e,d;

   e = labs(64887L);
   printf("Value for e = %ld\n", a);

   d = labs(-1003080L);
   printf("Value for d = %ld\n", b);
   
   return(0);
}

OUTPUT:-

Value for e = 64887
Value for d = 1003080

Sunday, October 4, 2020

, ,

fput() program in C programming language

 fput() function in c programming is a file string function which is used to print a string to the file.

fput() function use two arguments pointer one of them is used for string and other one for file. 

A null terminated string pointed by str in to a file.A null character is not written into a given and at the time of success it returns the 0 and  EOF or -1.

fput() program in c language : -


#include<stdio.h>
#include<string.h>

int main()
{
    char str[50];
    FILE *fp;
    fp = fopen("yourfile2.txt", "w");

    if(fp == NULL)
    {
        printf("Error for opening file\n");
        exit(1);
    }

    printf("Testing with the help of fputs() function: \n\n");
    printf("To stop reading press Ctrl+Z in windows and Ctrl+D in Linux :");

    while( gets(str) != NULL )
    {
        fputs(str, fp);
    }

    fclose(fp);
    return 0;
}


Output :-

fput() program in C programming language
fput() program in C programming language



Saturday, October 3, 2020

,

bsearch function program in c programming language

 bsearch function program in c language

bsearch function in c used to perform the search in the array and array will be sorted by the ascending order.

 A pointer will be used to sorted the array and the base point to the initial element for the given array and the key pointed to the object that containing the value that going to be sorted with the comparison function.

Return value for the bsearch program:-

If search goes successful, bsearch() returns a pointer to an identical element of the array. If two or more elements are equal, the element pointed to that it is not  specified.

If
search goes unsuccessful finding the key, bsearch() returns NULL.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
 
#define NOSTRING 5
#define SIZE 26
 
int string_compare(void const *, void const *);
 
int main(void)
{
    int i;
    char strings[NOSTRING][SIZE];
    char search_str[SIZE];
    char *status;
 
    /* read strings from the user */
    printf("User, write in %d character strings...\n", NOSTRING);
    for (i= 0; i < NOSTRINGS; i++)
        fgets(strings[i], SIZE, stdin);
 
    printf("\n\n");
 
    /* display array of unsorted strings */
    printf("Before Sorting:\n");
    for (i = 0; i < NOSTRING; i++)
        fputs(strings[i], stdout);
 
    /* sort the array */
    qsort(strings, NOSTRING, SIZE, string_compare);
    printf("\n");
 
    /* array of sorted strings */
    printf("After Sorting:\n");
    for (i = 0; i < NOSTRING; i++)
        printf(strings[i], stdout);
 
    printf("\n\n");
 
    /* read the string to be searched for */
    printf("programer write about string you wish to search for...\n");
    fgets(search_str, SIZE, stdin);
    printf("\n");
 
    /* calling bsearch() to locate given string in the array */
    status = bsearch(search_str, strings, NOSTRINGS, SIZE, 
                       string_compare);
 
    /* verify if given value is found */
    assert(status != NULL);
    printf("your desired string is found!\n");
 
    return 0;
}
 
int string_compare(void const *str1, void const *str2)
{
    return strcmp((char *)str1, (char *)str2);
}

OUTPUT : -

bsearch function program in c programming language
 bsearch function program in c programming language

Friday, October 2, 2020

, ,

Strcspn function in c with example program

 Strcspn in c

Strcspn function in c programming use to find the first character in the given string that is equal to the character of the other string.
 
Strcspn function in c with example program
 Strcspn function in c with example program
 
 
Null characters are not included in this string search. 
 
strcspn function also know the end of the string with the null character (/0) 
 
This function return the index of the first character in the string.
 

Example

#include <stdio.h>
#include <string.h>
 
#define SIZE 40
 
int main(void)
{
  char string[SIZE] = "source string";
  char * substring = "alax";
 
  printf( "The first %i characters in the string \"%s\" "
          "are not in the string \"%s\" \n",
            strcspn(string, substring), string, substring);
 
}

OUTPUT

the first 10 character from string1("source string") are not in the given sting2 ("alax")

, , ,

strchr function in c with program example

strchr example in c

strchr function in c programming find the first appearance of the character in the string.
 
strchr function in c with program example
strchr function in c with program example

 
 
 The character in c programming function can be null character (/0) and the ending null char. also include in this string search. 
 
A string argument also contain the null character so that the end of the string can be noticed.
 
Return value for strchr program in c  

strchr function return a pointer if specified character are appears in the given string otherwise it return the NULL.
 

Example Program for strchr function in c

 
#include <stdio.h>
#include <string.h>
 
 
int main(void)
{
  char buffer1[SIZE] = "computer program";
  char * ptr;
  int    ch = 'p';
 
  ptr = strchr( buffer1, ch );
  printf( "The first occurrence of %c in '%s' is '%s'\n",
            ch, buffer1, ptr );
 
}


Tuesday, September 29, 2020

, ,

How To Write strcpy Program In C / strcpy function in c

 strcpy function in c programming use to copy a sting from one variable to another variable. This article will help you to teach how to write the strcpy program in c programming.

 

How to Write strcpy Program In C
How to Write strcpy Program In C

strcpy is an library function in c so you have to define <string.h> Library in header of c program and this function return copied value.

Syntax:-

char *strcpy(char *val1, const char *val2);

strcpy program in c programming

#include <stdio.h>
#include <string.h> //string library 

int main() {
   char val1[20] = "strcpy C program";
   char val2[20];

   // copying str1 to str2
   strcpy(val2, val1);

   puts(str2); // Output(strcpy C program)

   return 0;
}


strcpy function can be used with ANSI/ISO 9899-1990 version.

Saturday, September 26, 2020

,

Written C Program for strcmp() and strncmp()

 Learn to usestrcmp() and strncmp()  Library function in c which is defined in <string.h>. 

Here str1 and str2 are two strings for comparison.

The strcmp Function compares str1 to str2 in c program. If str1 is bigger than or adequate to str2, then str1 is a smaller amount than or greater than zero, which returns zero.

The function strncmp in c compares str2 to the n characters of the str1 string. The return value of strncmp is adequate to strcmp.

The positive and negative value given by the strncmp function is that the numerical difference between the primary mismatched characters within the strings.

Str1 and str2 are often character array variables or string constants.

C program for strcmp() and strncmp()


#include <stdio.h>
#include <string.h>

int main()
{
   char str1[ ] = "Computerinhindi"; //initialize character array
   char str2[ ] = "Computerinhindi"; 
   char str3[ ] = "Computerinhindi"; 

   printf("str1 = %s\n"
          "str2 = %s\n"
          "str3 = %s\n\n", str1, str2, str3);

   //comparing given strings
   printf("strcmp(str1, str2) = %d\n", strcmp(str1, str2));
   printf("strcmp(str2, str3) = %d\n", strcmp(str2, str3));
   printf("strcmp(str3, str1) = %d\n\n", strcmp(str3, str1));

   //comparing n characters with help of library function
   printf("strncmp(str1, str3, 3) = %d\n", strncmp(str1, str3, 3));
   printf("strncmp(str1, str3, 6) = %d\n", strncmp(str1, str3, 6));
   printf("strncmp(str3, str1, 6) = %d\n\n", strncmp(str3, str1, 6));

   return 0;
}

OUTPUT For Written C Program for strcmp()

 and strncmp(): - 

C Program for strcmp()  and strncmp(): -
strcmp() and strncmp()


Wednesday, September 23, 2020

,

How to use strlen() function in c


This c program example will help you to find the length of string in c programming with the help of strlen() function in c :-

 

How to use strlen() function in c
How to use strlen() function in c

 

main()
{
    char s1[20];
    int len;
    printf("\nEnter a string to find it's length: ");
    gets(s1);
    len = strlen(s1);
    printf("\ngive the length of string: %d", len);
    getch();
}


Output

Enter a string to find it's length:-functioninc

give the length of string:-11

Thursday, September 10, 2020

C program to seek out the length of the string with or without using strlen function in c


 Here we'll write a C program to seek out the length of the string. The length of the "Computerinhindi" string is 15.

C program to seek out the length of the string with or without using strlen function in c
C program to seek out the length of the string with or without using strlen function in c
Find string length without using string library functions in C programming

To find the length, use for loop or while loop. The program below uses the for loop to check the string length in c program.

#include
int main()
{
     char st[200];
     int i;

     printf("Enter a string: ");
     scanf("%[^\n]",str);

     for(i=0;st[i]!='\0';i++);

     printf("length of given string is=%d",i);

     return 0;
}

The variable i is initialized with 0 inside the for loop. Now the condition str [i]! = '\ 0' are going to be checked for every character within the string. When the condition is true, the worth of the variable i will be able to increment.  


At the top of the string, it encounters a null character ("\ 0") therefore the condition becomes false and therefore the value of the variable i will be able to not increment, loop terminated. Finally, we get the length of the string without employing a built-in strlen () function in C program.

C Program to check String length using strlen()

  •  The strlen () function returns the length of the string passed.
  •  strlen () doesn't count the null character "\ 0"
  •  the specified header file may be a string.
#include
#include
int main()
{
     char a[20]="Computerinhindi";
     char b[20]={'C','O','R','N','N','E','R','\0'};
     char c[20];

     printf("Enter a string: ");
     scanf("%[^\n]",c);

     printf("Length of given string in c program %s =%ld\n",a,strlen(a));
     printf("Length of given string %s =%ld\n",b,strlen(b));
     printf("Length of string in c program %s =%ld\n",c,strlen(c));

     return 0;
 }

Here is that the C program to seek out the length of the string, to require user input we will use gets () or fgets (). The fgets () function also returns the newline character to the string. 

If you employ the fgets () function as shown below, you'll get result = (actual length + 1). The gets () takes an equivalent input length as [^ \ n] took. For an equivalent string, gets () gives the length 14 except for fgets () gives the length 15.