Category: Strings in C

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

sprintf function Demo 2

sprintf function Demo

Understand how sprintf function works in C programming language with the help of an example. Here we demonstrate sprintf function in C language. If you have any questions, do feel free to comment. #include #include void main(){ char *str; char *first=”Learn”, *second = “C”, *third=”Online”; clrscr(); sprintf(str,”%s%s%s.com is a great site”, first, second, third); printf(“%s”,str); getch(); } Output: LearnCOnline.com is a great site Also See: Explanation on sprintf function

sscanf function Demo 1

sscanf function Demo

Understand how sscanf function works in C programming language with the help of an example. Here we demonstrate sscanf function in C language. If you have any questions, do feel free to comment. #include<stdio.h> #include<conio.h> int main(){ char *str = “Learn C Online”; char *first, *second, *third; clrscr(); sscanf(str,”%s %s %s”, first, second, third); printf(“%s”,first); printf(“\n%s”,second); printf(“\n%s”,third); return 0; } Output: Learn C Online Also See: Explanation on sscanf function

sscanf and sprintf functions 19

sscanf and sprintf functions

sscanf() function is used to extract strings from the given string. Consider, char *str = “Learn C Online”; If we want to extract “Learn”, “C” and “Online” in a different variable then it can be done using sscanf function. Syntax: sscanf(characterArray, “Conversion specifier”, address of variables); This will extract the data from the character array according to the conversion specifier and store into the respective variables. sscanf() will read subsequent characters until a whitespace is found (whitespace characters are blank, newline and tab). Let us understand this using an example. char *str = “Learn C Online”; char *first, *second, *third;...

String handling functions in C 22

String handling functions in C

In this article, you will understand different string handling functions in C. By the end of this article, you will learn how to write C programs to perform the following string operations –  find the length of a string, compare 2 strings, copy one string to another, concatenate 2 strings, etc. Following are some of the useful string handling functions in C. strlen() strcpy() strncpy() strcat() strncat() strcmp() strncmp() strcmpi() strncmpi() These string handling functions are defined in string.h header file. Hence you need to include this header file whenever you use these string handling functions in your C program....

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