Author: learnconline

C program to read a string 4

C program to read a string and replaces every 5th character with “X”

The below program in C will accept a string from user. It will then process the string and replace every 5th character of the string with a character “X”. Here is the C code. #include<stdio.h> #include<conio.h> #include<string.h> void main() { char *str; int i; clrscr(); printf(“Enter a string: “); gets(str); for(i=0;i<strlen(str);i++) { if((i+1)%5 == 0) { str[i] = ‘X’; } } puts(str); getch(); } C Output Enter a string: This is a C Program ThisXis aXC PrXgram C Programming is brought you by LearnCOnline.com

Sort character array in ascending order in C 7

C program to sort character array (string) in ascending order

Below is the simple C program that will accept the character string from the user, calculates the length of the string and performs sorting operation on the string. The program will sort the character array in ascending order and display the result string (character array). Here we go, #include<stdio.h> #include<conio.h> void main() { clrscr(); int i,j; char *arr; //accept character array string from user printf(“Enter character string: “); gets(arr); //get the length of array in the variable counter int counter = 0; while(arr[counter] != NULL){ counter = counter + 1; } //apply ascending sorting algorithm for(i=0; i<counter; i++) { for(j=i;...

sizeof operator in C 4

sizeof operator in C

sizeof operator in C is an unary operator which can be used to find the size of the data-type or expression or a variable. It returns the size of the memory allocated (in Bytes) by the compiler. Lets say, we want to find the size of the memory allocated to the data-type int. We can find it by using sizeof operator as shown in the below code: #include<stdio.h> #include<conio.h> void main() { clrscr(); int sizeOfInt; sizeOfInt = sizeof(int); printf(“%d Bytes”, sizeOfInt); getch(); } Output: 2 Bytes As you can see, the above code calculated the size of int data type...

Program in C to find weekday based on weekday number 1

Program in C to find weekday based on weekday number

The below program in C will find out weekday based on the weekday number input. We will be following the below logic. 1 -> Sunday 2 -> Monday 3 -> Tuesday 4 -> Wednesday 5 -> Thursday 6 -> Friday 7 -> Saturday Here we are accepting the week day number as input from the user and use switch statement to display the weekday. Below is the program in C. #include<stdio.h> #include<conio.h> void main(){ int day; clrscr(); printf(“Enter the day number: “); scanf(“%d”, &day); switch(day){ case 1: printf(“\nIts Sunday”); break; case 2: printf(“\nIts Monday”); break; case 3: printf(“\nIts Tuesday”); break;...

Operations on Pointers in C 0

Operations on Pointers in C

The only operations that can be carried out on pointers in C are summarized below: A pointer variable in C can be assigned the address of an ordinary variable (e.g., pv = &v) A pointer variable can be assigned the value of another pointer variable (e.g., pv = px) provided both the pointers point to objects of the same data type A pointer variable in C can be assigned a null (zero) value (e.g., pv = NULL, where NULL is a symbolic constant that represents the value 0) An integer quantity can be added to or subtracted from a pointer...

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet