Found 1339 Articles for C

C Program to find minimum occurrence of character in a string

Bhanu Priya
Updated on 24-Mar-2021 14:12:33

2K+ Views

An array of characters is called a string.DeclarationFollowing is the declaration for declaring an array is as follows −char stringname [size];For example − char string[50]; string of length 50 charactersInitializationUsing single character constant −char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}Using string constants −char string[10] = "Hello":;Accessing − There is a control string "%s" used for accessing the string till it encounters ‘\0’.Finding minimum occurrenceThe logic to find minimum occurrence of a character in a given string is as follows −for(i=0; i

C program to print array of pointers to strings and their address

Bhanu Priya
Updated on 19-Mar-2021 10:04:52

8K+ Views

First, let us understand what are the arrays of pointers in C programming language.Arrays of pointers: (to strings)It is an array whose elements are ptrs to the base add of the string.It is declared and initialized as follows −char *a[ ] = {"one", "two", "three"};Here, a[0] is a pointer to the base add of string "one".         a[1] is a pointer to the base add of string "two".         a[2] is a pointer to the base add of string "three".AdvantagesThe advantages of array of pointers are explained below −Unlink the two dimensional array of characters, in ... Read More

What is strrev() Function in C language?

Bhanu Priya
Updated on 19-Mar-2021 10:01:46

4K+ Views

An array of characters is called a string.DeclarationThe syntax for declaring an array is as follows −char stringname [size];For example − char string[50]; string of length 50 charactersInitializationUsing single character constant −char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}Using string constants −char string[10] = "Hello":;Accessing − There is a control string "%s" used for accessing the string till it encounters ‘\0’.The strrev( ) FunctionThis function is used for reversing a string.The reversed string is stored in the same string.SyntaxThe syntax for strrev() function is as follows −strrev (string)ExampleThe following program shows the usage of strrev() function.#include main ( ... Read More

What is strncpy() Function in C language?

Bhanu Priya
Updated on 19-Mar-2021 09:50:54

765 Views

The C library function char *strncpy(char *dest, const char *src, size_t n) copies up to n characters from the string pointed to, by src to dest. In a case where, the length of src is less than that of n, the remainder of dest will be padded with null bytes.An array of characters is called a string.DeclarationFollowing is the declaration for an array −char stringname [size];For example − char string[50]; string of length 50 charactersInitializationUsing single character constant −char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}Using string constants −char string[10] = "Hello":;Accessing − There is a control string ... Read More

Explain pointers and two-dimensional array in C language

Bhanu Priya
Updated on 17-Mar-2021 10:23:57

7K+ Views

Pointer is a variable that stores the address of another variable.FeaturesPointer saves the memory space.Execution time of pointer is faster because of direct access to memory location.With the help of pointers, the memory is accessed efficiently, i.e., memory is allocated and deallocated dynamically.Pointers are used with data structures.Pointers and two dimensional arraysMemory allocation for a two-dimensional array is as follows −int a[3] [3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};a[1] [2] = *(1234 + 1*3+2) = *(1234 + 3+2) = *(1234 + 5*4) // 4 is Scale factor = * (1234+20) = *(1254) a[1] [2] = 6ExampleFollowing ... Read More

How to send an entire array as an argument in C language?

Bhanu Priya
Updated on 17-Mar-2021 10:00:40

298 Views

An array is a group of related items that is stored with a common name.Declaring arrayThe syntax for declaring an array is as follows −datatype array_name [size];InitializationAn array can be initialized in two ways, which are as follows −Compile time initialization.Runtime initialization.An array can also be initialized at the time of declaration as follows −int a[5] = {100, 200, 300, 400, 500};FunctionA function is a self-contained block that carries out a specific well-defined task. The two ways of passing the arrays as arguments to functions are as follows −Sending an entire array as an argument to function.Sending the individual elements ... Read More

Explain scope rules related to the functions in C language

Bhanu Priya
Updated on 15-Mar-2021 15:22:35

721 Views

Scope rules are related to the following factors −Accessibility of a variables.Period of existence of a variable.Boundary of usage of variables.Scope rules related to functions are as followsFunction which is a self-contained block that performs a particular task.Variables that are declared within the function body are called local variables.These variables only exist inside the specific function that creates them. They are unknown to other functions and to the main functions too.The existence of local variables ends when the function completes its specific task and returns to the calling point.Example 1Following is the C program for scope rules related to functions ... Read More

Write a C program to reverse array

Bhanu Priya
Updated on 15-Mar-2021 15:04:15

3K+ Views

An array is a group of related items that store with a common name.SyntaxThe syntax is as follows for declaring an array −datatype array_name [size];InitializationAn array can also be initialized at the time of declaration −int a[5] = { 10, 20, 30, 40, 50};Reversing array in CWe can reverse array by using swapping technique.For example, if 'P' is an array of integers with four elements −P[0] = 1, P[1] = 2, P[2] = 3 and P[3]=4Then, after reversing −P[0] = 4, P[1] = 3, P[2] = 2 and P[3]=1ExampleFollowing is the C program to reverse an array −#include int ... Read More

What are the predefined functions in C language?

Bhanu Priya
Updated on 15-Mar-2021 15:01:55

11K+ Views

Functions are broadly classified into two types, which are as follows −Predefined functionsUser defined functionsPredefined (or) library functionsThese functions are already defined in the system libraries.Programmer will reuse the already present code in the system libraries to write error free code.But to use the library functions, user must be aware of syntax of the function.Example −sqrt() function is available in math.h library and its usage is −y= sqrt (x) x number must be positive eg: y = sqrt (25) then ‘y’ = 5printf ( ) present in stdio.h library.clrscr ( ) present in conio.h library.ExampleGiven below is the C program ... Read More

C Program to reverse a given number using Recursive function

Bhanu Priya
Updated on 15-Mar-2021 10:21:05

12K+ Views

"Recursive function" is something which calls itself again in the body of the function.For example, A function fact ( ), which computes the factorial of an integer ‘N’, which is the product of all whole numbers from 1 to N.fact ( ) with an argument of 1 (or) 0, the function returns 1. otherwise, it returns n*fact (n-1), this happens until ‘n’ equals 1.Fact (5) =5* fact (4)    =5*4*3* fact (3)    =5*4*3*2* fact (2)    =5*4*3*2*1 fact (1)    =5*4*3*2*1    = 120.ExampleFollowing is the C program for use of recursive function to reverse a number −#include main ... Read More

Advertisements