Category: Programming Examples in C

c programming write text 2

Program to write a text to a file

We will write a program in C that will accept a string from the user and write the string in a file. In this program, we will use fopen function in order to open a file. We will also use fputs function which will write a string to a file. Below is the program: #include<stdio.h> #include<conio.h> void main() { char *str; FILE *fp; //Open the file in write mode fp = fopen(“write.txt”, “w”); printf(“Enter a string: “); gets(str); //write string in a file fputs(str, fp); getch(); } C Programming is brought you by LearnCOnline.com

2

Program to display right angled triangle of star (asterisk)

Below the program in C that will display right angled triangle of star/asterisk. The program will use for and while loop to display the star/asterisk on the screen. Expected Output * ** *** Here is the code, #include<stdio.h> #include<conio.h> void main() { int i, counter; clrscr(); for(i=0; i<3; i++) { counter = 0; while(counter <= i){ printf(“*”); counter++; } printf(“\n”); } getch(); } This C Program is brought you by LearnCOnline.com

C programming read text from a file and display it on screen 1

Program to read text from a file and display it on screen

We will write a program in C that will read a text file character by character and displays it on the screen. In order to achieve it, we will use fopen function that will open the file from the disk. We will also use fgetc function which will read the file character by character and then display the content of the string character by character. #include<stdio.h> #include<conio.h> void main() { FILE *fp; char ch; fp = fopen(“file1.C”,”r”); while(1) { ch = fgetc(fp); if (ch == EOF) break; printf(“%c”, ch); } getch(); fclose(fp); } C Programming is brought you by LearnCOnline.com

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;...