Anda di halaman 1dari 13

C Program to Find Number of Digits in a Number

#include <stdio.h>
int main()
{
int n,count=0;
printf("Enter an integer: ");
scanf("%d", &n);
while(n!=0)
{
n/=10;
/* n=n/10 */
++count;
}
printf("Number of digits: %d",count);
}

Output
Enter an integer: 34523
Number of digits: 5

Source Code to Find the Frequency of Characters


#include <stdio.h>
int main(){
char c[1000],ch;
int i,count=0;
printf("Enter a string: ");
gets(c);
printf("Enter a characeter to find frequency: ");
scanf("%c",&ch);
for(i=0;c[i]!='\0';++i)
{
if(ch==c[i])
++count;
}
printf("Frequency of %c = %d", ch, count);
return 0;
}

Output
Enter a string: This website is awesome.
Enter a frequency to find frequency: e
Frequency of e = 4

Source Code to Remove Characters in String Except


Alphabets
#include<stdio.h>
int main(){
char line[150];
int i,j;
printf("Enter a string: ");
gets(line);
for(i=0; line[i]!='\0'; ++i)
{
while (!((line[i]>='a'&&line[i]<='z') || (line[i]>='A'&&line[i]<='Z' || line[i]=='\0')))
{
for(j=i;line[j]!='\0';++j)
{
line[j]=line[j+1];
}
line[j]='\0';
}
}
printf("Output String: ");
puts(line);
return 0;
}

Output
Enter a string: p2'r"o@gram84iz./
Output String: programiz

Source Code to Check Character is an alphabet or


not
/* C programming code to check whether a character is alphabet or not.*/
#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");
scanf("%c",&c);
if( (c>='a'&& c<='z') || (c>='A' && c<='Z'))
printf("%c is an alphabet.",c);
else
printf("%c is not an alphabet.",c);
return 0;
}

Output 1
Enter a character: *
* is not an alphabet
Enter a character: K
K is an alphabet

C Program to Replace Lowercase


Characters by Uppercase & Vice-Versa
1. #include <stdio.h>
2. #include <ctype.h>
3.
4. void main()
5. {
6.

char sentence[100];

7.

int count, ch, i;

8.
9.

printf("Enter a sentence \n");

10.

for (i = 0; (sentence[i] = getchar()) != '\n'; i++)

11.

12.

13.

14.

sentence[i] = '\0';

15.

/* shows the number of chars accepted in a sentence */

16.

count = i;

17.

printf("The given sentence is : %s", sentence);

18.

printf("\n Case changed sentence is: ");

19.

for (i = 0; i < count; i++)

20.

21.

ch = islower(sentence[i])? toupper(sentence[i]) :

22.

tolower(sentence[i]);

23.

putchar(ch);

24.
25.

}
}

Enter a sentence
wELCOME tO sANFOUNDRY
The given sentence is
: wELCOME tO sANFOUNDRY
Case changed sentence is: Welcome To Sanfoundry

C Program to Count the Total Number of


Words in the Sentence using Command
Line Argument
1. #include <stdio.h>
2.
3. int main(int argc, char *argv[])
4. {
5.

int i = 0;

6.
7.

/* If no sentence is given in the command line */

8.

if (argc == 1)

9.

10.

printf("\n No sentence given on command line");

11.

return;

12.

13.

else

14.

15.

printf("\nThe words in the sentence are:");

16.

/*

17.

* From argv[1] to argv[argc -1] calculate the number of arguments

18.

*/

19.

for (i = 1;i < argc;i++)

20.

21.

printf("\n%s", argv[i]);

22.

23.

printf("\n\nTotal number of words:");

24.

printf(" %d", argc-1);

25.

26.

27.
28.
29.
30.
31.
32.
33.
34.

The words in the sentence are:


Welcome
to
C
Class
Total number of words: 4

Source Code to Access Array Elements Using


Pointer
#include <stdio.h>
int main(){
int data[5], i;
printf("Enter elements: ");
for(i=0;i<5;++i)
scanf("%d",data+i);
printf("You entered: ");
for(i=0;i<5;++i)
printf("%d\n",*(data+i));
return 0;
}

Output
Enter elements: 1
2
3
5
4
You entered: 1
2
3
5
4

Source Code to Sort Words in Dictionary Order


#include<stdio.h>
#include <string.h>
int main(){
int i,j;
char str[10][50],temp[50];
printf("Enter 10 words:\n");
for(i=0;i<10;++i)
gets(str[i]);
for(i=0;i<9;++i)
for(j=i+1;j<10 ;++j){
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf("In lexicographical order: \n");
for(i=0;i<10;++i){
puts(str[i]);
}
return 0;
}

Output

Passing pointers as parameters:


A program that swaps integers using a function that accepts pointers to the integers to be swapped:

#include <stdio.h>
void swap(int * q,int * p)
{
int temp = *p;
*p = *q;
*q = temp;
}
int main()
{
int a = 10, b = 2, x = 3, y = 5;
printf("a,b,x,y: %d,%d,%d,%d\n", a, b, x, y);
swap(&x, &y);
swap(&a, &b);
printf("a,b,x,y: %d,%d,%d,%d\n", a, b, x, y);
}
It will produces following result:

a,b,x,y: 10,2,3,5
a,b,x,y: 2,10,5,3

Passing arrays as parameters:


A program that reads elements and assign them in an array. Then, it prints the array elements out.

#include <stdio.h>
void get_array(int a[], int size);
void prt_array(int a[], int size);
#define SIZE 10
int main()
{
int a[SIZE];
get_array(a, SIZE);
prt_array(a, SIZE);
printf("\n");
return 0;
}
void get_array(int a[], int size)
{
int i;
printf("Enter 10 values, after each value press enter:\n " );
for (i = 0; i < size; i++)
scanf("%d", &a[i]);
}
void prt_array(int a[], int size)
{
int i;

printf("Printing all values :\n");


for (i = 0; i < size; i++)
printf("%d\n", a[i]);
}
This will produce something like:

Enter 10 values, after each value press enter:


10
2
3
4
5
6
7
8
9
10
Printing all values :
10
2
3
4
5
6
7
8
9
10

Program : How to Add Two Numbers Using Pointer ?


#include<stdio.h>
int main() {
int *ptr1, *ptr2;
int num;
printf("\nEnter two numbers : ");
scanf("%d %d", ptr1, ptr2);
num = *ptr1 + *ptr2;
printf("Sum = %d", num);
return (0);
}

Output :

Enter two numbers : 2 3


1 Sum = 5
2

C Program to display array elements with addresses :

1 #include<stdio.h>
2 #include<stdlib.h>
3 #define size 10
4
5 int main() {
6

int a[3] = { 11, 22, 33 };

7
8

printf("\n a[0] ,value=%d : address=%u", a[0], &a[0]);

printf("\n a[1] ,value=%d : address=%u", a[1], &a[1]);

10

printf("\n a[2] ,value=%d : address=%u", a[2], &a[2]);

11
12

return (0);

13 }

Output :

1 a[0] ,value=11 : address=2358832


2 a[1] ,value=22 : address=2358836
3 a[2] ,value=33 : address=2358840

Program : Length of the String using Pointer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

#include<stdio.h>
#include<conio.h>
int string_ln(char*);
void main() {
char str[20];
int length;
clrscr();
printf("\nEnter any string : ");
gets(str);
length = string_ln(str);
printf("The length of the given string %s is : %d", str, length);
getch();
}
int string_ln(char*p) /* p=&str[0] */
{
int count = 0;
while (*p != '\0') {
count++;
p++;
}
return count;
}

Output :

1 Enter the String : pritesh


2 Length of the given string pritesh is : 7

Program : Finding Inverse of a 3 X 3 Matrix

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

#include<stdio.h>
//
a
3
i
i
void reduction(float a[][6], int size, int pivot, int col) {
int i, j;
float factor;
factor = a[pivot][col];
for (i = 0; i < 2 * size; i++) {
a[pivot][i] /= factor;
}
for (i = 0; i < size; i++) {
if (i != pivot) {
factor = a[i][col];
for (j = 0; j < 2 * size; j++) {
a[i][j] = a[i][j] - a[pivot][j] * factor;
}
}
}
}
void main() {
float matrix[3][6];
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 6; j++) {
if (j == i + 3) {
matrix[i][j] = 1;
} else {
matrix[i][j] = 0;
}
}
}
printf("\nEnter a 3 X 3 Matrix :");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%f", &matrix[i][j]);
}
}
for (i = 0; i < 3; i++) {
reduction(matrix, 3, i, i);
}
printf("\nInvers Matrix");
for (i = 0; i < 3; i++) {
printf("\n");
for (j = 0; j < 3; j++) {
printf("%8.3f", matrix[i][j + 3]);

53
}
54 }
55 }

Output :

1
2
3
4
5
6
7
8
9

Enter a 3 X 3 Matrix
131
112
234
Invers Matrix
2.000 9.000 -5.000
0.000 -2.000 1.000
-1.000 -3.000 2.000

Anda mungkin juga menyukai