All C Programs
All C Programs
All C Programs
main() { char ch; printf("Enter a character\n"); scanf("%c", &ch); switch(ch) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': printf("%c is a vowel.\n", ch); break; default: printf("%c is not a vowel.\n", ch); } return 0; }
Page 1
Common C Programs
C programming code
#include <stdio.h> main() { int n, sum = 0, remainder; printf("Enter an integer\n"); scanf("%d",&n); while(n != 0) { remainder = n % 10; sum = sum + remainder; n = n / 10; } printf("Sum of digits of entered number = %d\n",sum); return 0; }
Page 2
Common C Programs
For example if the input is 98, sum(variable) is 0 initially 98%10 = 8 (% is modulus operator which gives us remainder when 98 is divided by 10). sum = sum + remainder so sum = 8 now. 98/10 = 9 because in c whenever we divide integer by another integer we get an integer. 9%10 = 9 sum = 8(previous value) + 9 sum = 17 9/10 = 0. So finally n = 0, loop ends we get the required sum.
Factorial program in c
Factorial program in c: c code to find and print factorial of a number, three methods are given, first one uses a for loop, second uses a function to find factorial and third using recursion. Factorial is represented using !, so five factorial will be written as 5!, n factorial as n!. Also n! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is defined as one i.e. 0!=1.
Page 3
Common C Programs
Page 4
Common C Programs
return(1); else return(n*factorial(n-1)); }
C programming code
#include <stdio.h> int main() { int a, b, x, y, t, gcd, lcm; printf("Enter two integers\n"); scanf("%d%d", &x, &y); a = x; b = y; while t = b = a = } (b != 0) { b; a % b; t;
gcd = a; lcm = (x*y)/gcd; printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd); printf("Least common multiple of %d and %d = %d\n", x, y, lcm); return 0; }
Page 5
Common C Programs
printf("Greatest common divisor of %ld and %ld = %ld\n", x, y, hcf); printf("Least common multiple of %ld and %ld = %ld\n", x, y, lcm); return 0; } long gcd(long a, long b) { if (b == 0) { return a; } else { return gcd(b, a % b); } }
Page 6
Common C Programs
C programming code
#include <stdio.h> int main() { int n, c, k; printf("Enter an integer in decimal number system\n"); scanf("%d", &n); printf("%d in binary number system is:\n", n); for (c = 31; c >= 0; c--) { k = n >> c; if (k & 1) printf("1"); else printf("0"); } printf("\n");
Page 7
Common C Programs
return 0; }
Above code only prints binary of integer, but we may wish to perform operations on binary so in the code below we are storing the binary in a string. We create a function which returns a pointer to string which is the binary of the number passed as argument to the function.
Compiler used:
Raghudathesh G P Asst Prof GMIT DVG Page 8
Common C Programs
GCC Output of program:
Page 9
Common C Programs
{ long result; result = factorial(n)/(factorial(r)*factorial(n-r)); return result; } long find_npr(int n, int r) { long result; result = factorial(n)/factorial(n-r); return result; } long factorial(int n) { int c; long result = 1; for( c = 1 ; c <= n ; c++ ) result = result*c; return ( result ); }
Output of program:
Page 10
Common C Programs
printf("Enter the value of x and y\n"); scanf("%d%d", &x, &y); printf("Before Swapping\nx = %d\ny = %d\n",x,y); temp = x; x = y; y = temp; printf("After Swapping\nx = %d\ny = %d\n",x,y); return 0; }
To understand above logic simply choose a as 7 and b as 9 and then do what is written in program. You can choose any other combination of numbers as well. Sometimes it's a good way to understand a program.
Page 11
Common C Programs
temp = *b; *b = *a; *a = temp; printf("After Swapping\nx = %d\ny = %d\n", x, y); return 0; }
Page 12
Common C Programs
y = x ^ y; x = x ^ y; printf("x = %d\ny = %d\n", x, y); return 0; }
C programming code
#include <stdio.h> main() { int n, reverse = 0; printf("Enter a number to reverse\n"); scanf("%d",&n); while (n != 0) { reverse = reverse * 10; reverse = reverse + n%10; n = n/10; } printf("Reverse of entered number is = %d\n", reverse); return 0; }
Output of program:
Palindrome Numbers
Palindrome number in c: A palindrome number is a number such that if we reverse it, it will not change. For example some palindrome numbers examples are 121, 212, 12321, -454. To
Raghudathesh G P Asst Prof GMIT DVG Page 13
Common C Programs
check whether a number is palindrome or not first we reverse it and then compare the number obtained with the original, if both are same then number is palindrome otherwise not. C program for palindrome number is given below.
Page 14
Common C Programs
* *** ***** ******* *********
We have shown five rows above, in the program you will be asked to enter the numbers of rows you want to print in the pyramid of stars.
C programming code
#include<stdio.h> main() { int row, c, n, temp; printf("Enter the number of rows in pyramid of stars you wish to see "); scanf("%d",&n); temp = n; for ( row = 1 ; row <= n ; row++ ) { for ( c = 1 ; c < temp ; c++ ) printf(" "); temp--; for ( c = 1 ; c <= 2*row - 1 ; c++ ) printf("*"); printf("\n"); } return 0; }
Output:
Page 15
Common C Programs
Consider the pattern * ** *** **** ***** to print above pattern see the code below:
#include<stdio.h> main() { int n, c, k; printf("Enter number of rows\n"); scanf("%d",&n); for ( c = 1 ; c <= n ; c++ ) { for( k = 1 ; k <= c ; k++ ) printf("*"); printf("\n"); } return 0; }
C programming code
#include <stdio.h> int main() { int n, c, k, space = 1; printf("Enter number of rows\n"); scanf("%d", &n); space = n - 1; for (k = 1; k <= n; k++) {
Page 16
Common C Programs
for (c = 1; c <= space; c++) printf(" "); space--; for (c = 1; c <= 2*k-1; c++) printf("*"); printf("\n"); } space = 1; for (k = 1; k <= n - 1; k++) { for (c = 1; c <= space; c++) printf(" "); space++; for (c = 1 ; c <= 2*(n-k)-1; c++) printf("*"); printf("\n"); } return 0; }
Page 17
Common C Programs
for ( count = 2 ; count <= n ; ) { for ( c = 2 ; c <= i - 1 ; c++ ) { if ( i%c == 0 ) break; } if ( c == i ) { printf("%d\n",i); count++; } i++; } return 0; }
There are many logic to check prime numbers, one given below is more efficient then above method. for ( c = 2 ; c <= (int)sqrt(n) ; c++ ) //only checking from 2 to square root of number is sufficient. There are many more efficient logic then written above.
Page 18
Common C Programs
int check_prime(int); main() { int n, result; printf("Enter an integer to check whether it is prime or not.\n"); scanf("%d",&n); result = check_prime(n); if ( result == 1 ) printf("%d is prime.\n", n); else printf("%d is not prime.\n", n); return 0; } int check_prime(int a) { int c; for ( c = 2 ; c <= a - 1 ; c++ ) { if ( a%c == 0 ) return 0; } if ( c == a ) return 1; }
C programming code
#include <stdio.h> main() { int number, sum = 0, temp, remainder; printf("Enter a number\n"); scanf("%d",&number); temp = number; while( temp != 0 ) { remainder = temp%10;
Page 19
Common C Programs
sum = sum + remainder*remainder*remainder; temp = temp/10; } if ( number == sum ) printf("Entered number is an armstrong number."); else printf("Entered number is not an armstrong number."); return 0; }
Output of program:
Fibonacci series in c
Fibonacci series in c programming: c program for Fibonacci series without and with recursion. Using the code below you can print as many number of terms of series as desired. Numbers of Fibonacci sequence are known as Fibonacci numbers. First few numbers of series are 0, 1, 1, 2, 3, 5, 8 etc, Except first two terms in sequence every other term is the sum of two previous terms, For example 8 = 3 + 5 (addition of 3, 5). This sequence has many applications in mathematics and Computer Science.
Page 20
Common C Programs
return 0; }
Output of program:
Common C Programs
C program to print Floyd's triangle:- This program prints Floyd's triangle. Number of rows of Floyd's triangle to print is entered by the user. First four rows of Floyd's triangle are as follows :1 23 456 7 8 9 10 It's clear that in Floyd's triangle nth row contains n numbers.
C programming code
#include<stdio.h> #include<conio.h> main() { int n, i,
c, a = 1;
printf("Enter the number of rows of Floyd's triangle to print\n"); scanf("%d",&n); for ( i = 1 ; i <= n ; i++ ) { for ( c = 1 ; c <= i ; c++ ) { printf("%d ",a); a++; } printf("\n"); } getch(); return 0; }
Output of program:
Page 22
Common C Programs
1 1 1 2 1 1 3 3 1
Pascal triangle in c
#include<stdio.h> long factorial(int); main() { int i, n, c; printf("Enter the number of rows you wish to see in pascal triangle\n"); scanf("%d",&n); for ( i = 0 ; i < n ; i++ ) { for ( c = 0 ; c <= ( n - i - 2 ) ; c++ ) printf(" "); for( c = 0 ; c <= i ; c++ ) printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c))); printf("\n"); } return 0; } long factorial(int n) { int c; long result = 1; for( c = 1 ; c <= n ; c++ ) result = result*c; return ( result ); }
For more patterns or shapes on numbers and characters see codes on following pages: Patterns programs Floyd triangle Pascal triangle program.
Page 23
Common C Programs
Output of program:
C programming code
#include <stdio.h> int main() { int array[100], maximum, size, c, location = 1; printf("Enter the number of elements in array\n"); scanf("%d", &size); printf("Enter %d integers\n", size); for (c = 0; c < size; c++) scanf("%d", &array[c]); maximum = array[0]; for (c = 1; c < size; c++) { if (array[c] > maximum) { maximum = array[c]; location = c+1; } } printf("Maximum element is present at location number %d and it's value is %d.\n", location, maximum); return 0; }
Common C Programs
#include <stdio.h> int main() { long array[100], *maximum, size, c, location = 1; printf("Enter the number of elements in array\n"); scanf("%ld", &size); printf("Enter %ld integers\n", size); for ( c = 0 ; c < size ; c++ ) scanf("%ld", &array[c]); maximum = array; *maximum = *array; for (c = 1; c < size; c++) { if (*(array+c) > *maximum) { *maximum = *(array+c); location = c+1; } } printf("Maximum element is present at location number %ld and it's value is %ld.\n", location, *maximum); return 0; }
C programming code
#include <stdio.h> int main() { int n, c, d, a[100], b[100];
Page 25
Common C Programs
printf("Enter the number of elements in array\n"); scanf("%d", &n); printf("Enter the array elements\n"); for (c = 0; c < n ; c++) scanf("%d", &a[c]); /* * Copying elements into array b starting from end of array a */ for (c = n - 1, d = 0; c >= 0; c--, d++) b[d] = a[c]; /* * Copying reversed array into original. * Here we are modifying original array, this is optional. */ for (c = 0; c < n; c++) a[c] = b[c]; printf("Reverse array is\n"); for (c = 0; c < n; c++) printf("%d\n", a[c]); return 0; }
Output of program:
Page 26
Common C Programs
int array[100], n, c, t, end; scanf("%d", &n); end = n - 1; for (c = 0; c < n; c++) { scanf("%d", &array[c]); } for (c = 0; c < n/2; c++) { t = array[c]; array[c] = array[end]; array[end] = t; end--; } printf("Reversed array elements are:\n"); for (c = 0; c < n; c++) { printf("%d\n", array[c]); } return 0; }
Page 27
Common C Programs
void reverse_array(int *pointer, int n) { int *s, c, d; s = (int*)malloc(sizeof(int)*n); if( s == NULL ) exit(EXIT_FAILURE); for ( c = n - 1, d = 0 ; c >= 0 ; c--, d++ ) *(s+d) = *(pointer+c); for ( c = 0 ; c < n ; c++ ) *(pointer+c) = *(s+c); free(s); }
C programming code
#include <stdio.h> main() { int array[100], position, c, n, value; printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); printf("Enter the location where you wish to insert an element\n"); scanf("%d", &position); printf("Enter the value to insert\n"); scanf("%d", &value); for ( c = n - 1 ; c >= position - 1 ; c-- ) array[c+1] = array[c]; array[position-1] = value; printf("Resultant array is\n");
Page 28
Common C Programs
for( c = 0 ; c <= n ; c++ ) printf("%d\n", array[c]); return 0; }
c programming code
#include <stdio.h> main() { int array[100], position, c, n; printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); printf("Enter the location where you wish to delete element\n"); scanf("%d", &position); if ( position >= n+1 ) printf("Deletion not possible.\n"); else { for ( c = position - 1 ; c < n - 1 ; c++ ) array[c] = array[c+1]; printf("Resultant array is\n"); for( c = 0 ; c < n - 1 ; c++ ) printf("%d\n", array[c]); } return 0; }
Common C Programs
C program to merge two arrays into third array: Arrays are assumed to be sorted in ascending order. You enter two short sorted arrays and combine them to get a large array.
Page 30
Common C Programs
sorted[i] = b[k]; k++; i++; } } else { for (; i < m + n;) { sorted[i] = a[j]; j++; i++; } } } }
Output of program:
Page 31
Common C Programs
C programming code
#include <stdio.h> main() { int m, n, c, d, first[10][10], second[10][10], sum[10][10]; printf("Enter the number of rows and columns of matrix "); scanf("%d%d", &m, &n); printf("Enter the elements of first matrix\n"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d", &first[c][d]); printf("Enter the elements of second matrix\n"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d", &second[c][d]); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) sum[c][d] = first[c][d] + second[c][d]; printf("Sum of entered matrices:-\n"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < n ; d++ ) printf("%d\t", sum[c][d]); printf("\n"); } return 0; }
Page 32
Common C Programs
Output of program:
C programming code
#include<stdio.h> main() { int m, n, c, d, matrix[10][10], transpose[10][10]; printf("Enter the number of rows and columns of matrix "); scanf("%d%d",&m,&n); printf("Enter the elements of matrix \n"); for( c = 0 ; c < m ; c++ ) { for( d = 0 ; d < n ; d++ ) {
Page 33
Common C Programs
scanf("%d",&matrix[c][d]); } } for( c = 0 ; c < m ; c++ ) { for( d = 0 ; d < n ; d++ ) { transpose[d][c] = matrix[c][d]; } } printf("Transpose of entered matrix :-\n"); for( c = 0 ; c < n ; c++ ) { for( d = 0 ; d < m ; d++ ) { printf("%d\t",transpose[c][d]); } printf("\n"); } return 0; }
Output of program:
Matrix multiplication in c
Matrix multiplication in c language: c program to multiply matrices (two dimensional array), this program multiplies two matrices which will be entered by the user. Firstly user will enter the order of a matrix. If the entered orders of two matrix is such that they can't be multiplied then an error message is displayed on the screen. You have already studied the logic to
Raghudathesh G P Asst Prof GMIT DVG Page 34
Common C Programs
multiply them in Mathematics. Matrices are frequently used while doing programming and are used to represent graph data structure, in solving system of linear equations and many more.
multiply[c][d] = sum; sum = 0; } } printf("Product of entered matrices:-\n"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < q ; d++ ) printf("%d\t", multiply[c][d]); printf("\n");
Page 35
Common C Programs
} } return 0; }
A 3 X 3 matrix multiply in c is shown as example below. Compiler used: GCC Output of program:
C programming code
#include <stdio.h> main() { char array[20] = "Hello World"; printf("%s\n",array); return 0; }
Page 36
Common C Programs
C programming code
#include <stdio.h> main() { char array[100]; printf("Enter a string\n"); scanf("%s", array); printf("You entered the string %s\n",array); return 0; }
Note that scanf can only input single word strings, to receive strings containing spaces use gets function.
String length
This program prints length of string, for example consider the string "c programming" it's length is 13. Null character is not counted when calculating string length. To find string length we use strlen function of string.h.
Page 37
Common C Programs
printf("Length of entered string is = %d\n",length); return 0; }
Output of program:
You can also find string length using pointer or without strlen function. Following program shows how to achieve this.
Common C Programs
This c program compares two strings using strcmp, without strcmp and using pointers. For comparing strings without using library function see another code below.
Page 39
Common C Programs
main() { char first[100], second[100], result; printf("Enter first string\n"); gets(first); printf("Enter second string\n"); gets(second); result = compare_string(first, second); if ( result == 0 ) printf("Both strings are same.\n"); else printf("Entered strings are not equal.\n"); return 0; } int compare_string(char *first, char *second) { while(*first==*second) { if ( *first == '\0' || *second == '\0' ) break; first++; second++; } if( *first == '\0' && *second == '\0' ) return 0; else return -1; }
Page 40
Common C Programs
return 0; }
C code
#include<stdio.h> #include<conio.h> #include<string.h> main() { char a[100], b[100];
Page 41
Common C Programs
printf("Enter the first string\n"); gets(a); printf("Enter the second string\n"); gets(b); strcat(a,b); printf("String obtained on concatenation is %s\n",a); getch(); return 0; }
Page 42
Common C Programs
*original = *add; add++; original++; } *original = '\0'; }
Reverse string
This program reverses a string entered by the user. For example if a user enters a string "reverse me" then on reversing the string will be "em esrever". We show you three different methods to reverse string the first one uses strrev library function of string.h header file and in second we make our own function to reverse string using pointers, reverse string using recursion and Reverse words in string. If you are using first method then you must include string.h in your program.
C programming code
/* String reverse in c*/
#include<stdio.h> #include<string.h> main() { char arr[100]; printf("Enter a string to reverse\n"); gets(arr); strrev(arr); printf("Reverse of entered string is \n%s\n",arr); return 0; }
/* Second method */
Page 43
Common C Programs
printf("Enter a string\n"); gets(string); reverse(string); printf("Reverse of entered string is \"%s\".\n", string); return 0; } void reverse(char *string) { int length, c; char *begin, *end, temp; length = string_length(string); begin = string; end = string; for ( c = 0 ; c < ( length - 1 ) ; c++ ) end++; for ( c = { temp = *end = *begin 0 ; c < length/2 ; c++ ) *end; *begin; = temp;
begin++; end--; } } int string_length(char *pointer) { int c = 0; while( *(pointer+c) != '\0' ) c++; return c; }
Page 44
Common C Programs
printf("%s\n",a); return 0; } void reverse(char *x, int beg, int end) { char a, b, c; if ( beg >= end ) return; c = *(x+beg); *(x+beg) = *(x+end); *(x+end) = c; reverse(x, ++beg, --end); }
Output of program:
Page 45
Common C Programs
printf("Enter the string to check if it is a palindrome\n"); gets(a); strcpy(b,a); strrev(b); if( strcmp(a,b) == 0 ) printf("Entered string is a palindrome.\n"); else printf("Entered string is not a palindrome.\n"); return 0; }
Output of program:
Palindrome number in c
#include <stdio.h> main() { int n, reverse = 0, temp; printf("Enter a number to check if it is a palindrome or not\n"); scanf("%d",&n); temp = n; while( temp != 0 ) { reverse = reverse * 10; reverse = reverse + temp%10; temp = temp/10; } if ( n == reverse ) printf("%d is a palindrome number.\n", n); else printf("%d is not a palindrome number.\n", n); return 0; }
Page 46
Common C Programs
main() { char text[100]; int begin, middle, end, length = 0; gets(text); while ( text[length] != '\0' ) length++; end = length - 1; middle = length/2; for( begin = 0 ; begin < middle ; begin++ ) { if ( text[begin] != text[end] ) { printf("Not a palindrome.\n"); break; } end--; } if( begin == middle ) printf("Palindrome.\n"); return 0; }
Page 47
Common C Programs
{ int check, length; char *reverse; length = string_length(string); reverse = (char*)malloc(length+1); copy_string(reverse, string); reverse_string(reverse); check = compare_string(string, reverse); free(reverse); if ( check == 0 ) return 1; else return 0; } int string_length(char *string) { int length = 0; while(*string) { length++; string++; } return length; } void copy_string(char *target, char *source) { while(*source) { *target = *source; source++; target++; } *target = '\0'; } void reverse_string(char *string) { int length, c; char *begin, *end, temp; length = string_length(string); begin = string; end = string; for ( c = 0 ; c < ( length - 1 ) ; c++ ) end++; for ( c = 0 ; c < length/2 ; c++ ) {
Page 48
Common C Programs
temp = *end; *end = *begin; *begin = temp; begin++; end--; } } int compare_string(char *first, char *second) { while(*first==*second) { if ( *first == '\0' || *second == '\0' ) break; first++; second++; } if( *first == '\0' && *second == '\0' ) return 0; else return -1; }
C programming code
#include <stdio.h> #include <string.h> int check_vowel(char); int main() { char s[100], t[100]; int i, j = 0; printf("Enter a string to delete vowels\n"); gets(s); for(i = 0; s[i] != '\0'; i++) { if(check_vowel(s[i]) == 0) { t[j] = s[i]; j++; } } //not a vowel
Page 49
Common C Programs
t[j] = '\0'; strcpy(s, t); //We are changing initial string
printf("String after deleting vowels: %s\n", s); return 0; } int check_vowel(char c) { switch(c) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': return 1; default: return 0; } }
Page 50
Common C Programs
{ ch = *temp; if ( !check_vowel(ch) ) { *pointer = ch; pointer++; } temp++; } *pointer = '\0'; pointer = start; strcpy(string, pointer); /* If you wish to convert original string */ free(pointer); printf("String after removing vowel is \"%s\"\n", string); return 0; } int check_vowel(char a) { if ( a >= 'A' && a <= 'Z' ) a = a + 'a' - 'A'; if ( a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u') return TRUE; return FALSE; }
C substring code
#include <stdio.h> #include <malloc.h> char* substring(char*, int, int); main() { char string[100], *pointer; int position, length; printf("Enter a string\n"); gets(string);
Page 51
Common C Programs
printf("Enter the position and length of substring\n"); scanf("%d%d",&position, &length); pointer = substring( string, position, length); printf("Required substring is \"%s\"\n", pointer); free(pointer); return 0; } /*C substring function: It returns a pointer to the substring */ char *substring(char *string, int position, int length) { char *pointer; int c; pointer = malloc(length+1); if (pointer == NULL) { printf("Unable to allocate memory.\n"); exit(EXIT_FAILURE); } for (c = 0 ; c < position -1 ; c++) string++; for (c = 0 ; c < length ; c++) { *(pointer+c) = *string; string++; } *(pointer+c) = '\0'; return pointer; }
Page 52
Common C Programs
char* substring(char*, int, int); main() { char string[100], *pointer; int position = 1, length = 1, temp, string_length; printf("Enter a string\n"); gets(string); temp = string_length = strlen(string); printf("Substring of \"%s\" are\n", string); while (position <= string_length) { while (length <= temp) { pointer = substring(string, position, length); printf("%s\n", pointer); free(pointer); length++; } temp--; position++; length = 1; } return 0; } /* Use substring function given in above c program*/
Page 53
Common C Programs
Substring code output:
C programming code
#include <stdio.h> #include <stdlib.h> #include <string.h> void sort_string(char*); main() { char string[100];
Page 54
Common C Programs
printf("Enter some text\n"); gets(string); sort_string(string); printf("%s\n", string); return 0; } void sort_string(char *s) { int c, d = 0, length; char *pointer, *result, ch; length = strlen(s); result = (char*)malloc(length+1); pointer = s; for ( ch = 'a' ; ch <= 'z' ; ch++ ) { for ( c = 0 ; c < length ; c++ ) { if ( *pointer == ch ) { *(result+d) = *pointer; d++; } pointer++; } pointer = s; } *(result+d) = '\0'; strcpy(s, result); free(result); }
There are two spaces in this string, so our program will print a string "c programming". It will remove spaces when they occur more than one time consecutively in string anywhere.
C programming code
#include <stdio.h>
Page 55
Common C Programs
int main() { char text[100], blank[100]; int c = 0, d = 0; printf("Enter some text\n"); gets(text); while (text[c] != '\0') { if (!(text[c] == ' ' && text[c+1] == ' ')) { blank[d] = text[c]; d++; } c++; } blank[d] = '\0'; printf("Text after removing blanks\n%s\n", blank); return 0; }
Page 56
Common C Programs
c++; } *(start+d)='\0'; printf("%s\n", start); free(start); return 0; }
strlwr, strupr in c
Here we will change string case with and without strlwr, strupr functions.
strlwr in c
#include<stdio.h> #include<string.h> main() { char string[] = "Strlwr in C"; printf("%s\n",strlwr(string)); return } 0;
strupr in c
Raghudathesh G P Asst Prof GMIT DVG Page 57
Common C Programs
#include<stdio.h> #include<string.h> main() { char string[] = "strupr in c"; printf("%s\n",strupr(string)); return } 0;
Page 58
Common C Programs
lower_string(string); printf("Entered string in lower case is \"%s\"\n", string); return 0; } void lower_string(char *string) { while(*string) { if ( *string >= 'A' && *string <= 'Z' ) { *string = *string + 32; } string++; } }
C programming code
#include<stdio.h> #include<string.h> #include<malloc.h> #include<conio.h> main() { char first[100], second[100], *temp; printf("Enter the first string "); gets(first); printf("Enter the second string "); gets(second); printf("\nBefore Swapping\n"); printf("First string: %s\n",first); printf("Second string: %s\n\n",second); temp = (char*)malloc(100); strcpy(temp,first); strcpy(first,second); strcpy(second,temp); printf("After Swapping\n"); printf("First string: %s\n",first); printf("Second string: %s\n",second); getch(); return 0;
Page 59
Common C Programs
}
Output of program
C programming code
#include<stdio.h> #include<string.h> main() { char string[100], ch; int c = 0, count[26] = {0}; printf("Enter a string\n"); gets(string);
Page 60
Common C Programs
while ( string[c] != '\0' ) { /* Considering characters from 'a' to 'z' only */ if ( string[c] >= 'a' && string[c] <= 'z' ) count[string[c]-'a']++; c++; } for ( c = 0 ; c < 26 ; c++ ) { if( count[c] != 0 ) printf("%c occurs %d times in the entered string.\n",c+'a',count[c]); } return 0; }
Page 61
Common C Programs
Output of program:
anagram in c
Anagram in c: c program to check whether two strings are anagrams or not, string is assumed to consist of alphabets only. Two words are said to be anagrams of each other if the letters from one word can be rearranged to form the other word. From the above definition it is clear that two strings are anagrams if all characters in both strings occur same number of times. For example "abc" and "cab" are anagram strings, here every character 'a', 'b' and 'c' occur only one time in both strings. Our algorithm tries to find how many times characters appears in the strings and then comparing their corresponding counts.
Page 62
Common C Programs
int check_anagram(char [], char []); int main() { char a[100], b[100]; int flag; printf("Enter first string\n"); gets(a); printf("Enter second string\n"); gets(b); flag = check_anagram(a, b); if (flag == 1) printf("\"%s\" and \"%s\" are anagrams.\n", a, b); else printf("\"%s\" and \"%s\" are not anagrams.\n", a, b); return 0; } int check_anagram(char a[], char b[]) { int first[26] = {0}, second[26] = {0}, c = 0; while (a[c] != '\0') { first[a[c]-'a']++; c++; } c = 0; while (b[c] != '\0') { second[b[c]-'a']++; c++; } for (c = 0; c < 26; c++) { if (first[c] != second[c]) return 0; } return 1; }
Page 63
Common C Programs
C programming code
#include <stdio.h> struct complex { int real, img; }; main() { struct complex a, b, c; printf("Enter a and b where a + ib is the first complex number.\n"); printf("a = "); scanf("%d", &a.real); printf("b = "); scanf("%d", &a.img); printf("Enter c and d where c + id is the second complex number.\n"); printf("c = "); scanf("%d", &b.real); printf("d = "); scanf("%d", &b.img);
Page 64
Common C Programs
c.real = a.real + b.real; c.img = a.img + b.img; if ( c.img >= 0 ) printf("Sum of two complex numbers = %d + %di\n",c.real,c.img); else printf("Sum of two complex numbers = %d %di\n",c.real,c.img); return 0; }
C programming code
#include<stdio.h> #include<conio.h> #include<dos.h> main() { struct date d; getdate(&d); printf("Current system date is %d/%d/%d",d.da_day,d.da_mon,d.da_year); getch(); return 0; }
Page 65
Common C Programs
Output of program
C programming code
#include<stdlib.h> main() { system("C:\\Windows\\System32\\ipconfig"); system("pause"); return 0; }
Page 66
Common C Programs
Output of program: ( In Windows XP )
Page 67
Common C Programs
scanf("%c",&ch); if (ch == 'y' || ch == 'Y') system("C:\\WINDOWS\\System32\\shutdown -s"); return 0; }
To shutdown immediately use "C:\\WINDOWS\\System32\\ shutdown /s /t 0". To restart use /r instead of /s.
You need to be logged in as root user for above program to execute otherwise you will get the message shutdown: Need to be root, now specifies that you want to shutdown immediately. '-P' option specifes you want to power off your machine. You can specify minutes as: shutdown -P "number of minutes" For more help or options type at terminal: man shutdown.
Page 68