Here is a list of programs done for the IC Lab in the first semester.
posted Dec 3, 2010, 10:23 AM by Neil Mathew
[
updated Dec 3, 2010, 10:29 AM
]
* Al ot of string functions are not working. strrev, strlwr, strupr, stricmp...
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
#include<stdio.h>
#include<string.h>
void StringER(char *S1, char *S2)
{
char S3[50];
char *S4;
printf("\n");
//Copy and Duplicate a String.
strcpy(S3," Samuel");
S4=strdup(S3); //returns a pointer
printf("\n Copied String : %s", S3);
printf("\n Duplicated String : %s", S4);
//printf("\n");
//Cases
//char *sL=strlwr("HELP");
//printf("\n HELP or %s",sL);
printf("\n");
//Comparison with strcmp() stricmp() | Others: strncmp() strnicmp()
if( strcmp(S1,S2)==0)
printf("\n String 1 and 2 are the same. Case Sensitive ");
//else if(B==0)
//printf("\n String 1 and 2 are the . Not Case Sensitive ");
else if ( strncmp(S1, S2, 2) == 0)
{printf("\n First 2 characters of String 1 and 2 are the same.");
printf(" Case Sensitive"); }
//else if ( strnicmp(S1,S2,2) == 0 )
/*printf("\n First 2 characters of String 1 and 2
are the same. Not Case Sensitive"); */
else
printf("\n String 1 and 2 are NOT the same. Case Sensitive ");
printf("\n");
//Concatenate with S3
strcat(S1,S3); // pointer and array both can be used. *S1 S3[]
printf("\n Concatenated String : %s",S1);
strncat(S2,S3,4);
printf("\n Concatenated String : %s",S2);
//printf("\n");
//reverse
//char *sR=strrev(S1);
//printf("\n Reversed String : %s",sR);
}
main()
{
char S1[50];
char S2[50];
printf(" Enter the String 1 : ");
gets(S1);
printf(" Enter the String 2 : ");
gets(S2);
StringER(S1,S2);
/* No & (ampersand) for Strings in scanf
and pointers - ptr=S1; unlike ptr=&a; */
}
|
OUTPUT:
Enter the String 1 : Hello
Enter the String 2 : HeLLo
Copied String : Samuel
Duplicated String : Samuel
First 2 characters of String 1 and 2 are the same. Case Sensitive
Concatenated String : Hello Samuel
Concatenated String : HeLLo Sam |
posted Dec 3, 2010, 10:19 AM by Neil Mathew
[
updated Dec 3, 2010, 10:20 AM
]
1
2
3
4
5
6
7
8
9
10
| main()
{
if(0) // 0 = false
printf(" checks if ==0 , if 0 false, ");
if(6) // 6 = non zero = true
printf(" checks if ==0 , if non zero true ");
} |
OUTPUT:
checks if ==0 , if non zero true
|
posted Dec 3, 2010, 10:17 AM by Neil Mathew
[
updated Dec 3, 2010, 10:19 AM
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="This is a line";
char *p;
p = strstr (str, " ");
puts (p);
p = strchr (str, 'i');
puts (p);
p = strrchr (str, 'i');
puts (p);
return 0;
}
|
OUTPUT:
is a line
is is a line
ine |
posted Dec 2, 2010, 4:32 PM by Neil Mathew
[
updated Dec 2, 2010, 4:49 PM
]
1: // file error handling 2: #include <stdio.h> 3: 4: #define ERR_OPEN_1 11 5: #define ERR_OPEN_2 12 6: #define ERR_READ 13 7: #define ERR_WRITE 14 8: 9: int f_err_report(int mode) { 10: if(mode==ERR_OPEN_1) printf("error opening file1n"); 11: if(mode==ERR_OPEN_2) printf("error opening file2n"); 12: else if(mode==ERR_READ) printf("error reading from file"); 13: else if(mode==ERR_WRITE) printf("error writing to file"); 14: exit(1); 15: } 16: 17: int main(int argc, char *argv[]) 18: { 19: if(argc<3) { printf("%s <file1> <file2>n",argv[0]); exit(1); } 20: char ch; 21: FILE *fp,*fp2; 22: if((fp=fopen(argv[1],"r"))==NULL) f_err_report(ERR_OPEN_1); 23: if((fp2=fopen(argv[2],"w"))==NULL) f_err_report(ERR_OPEN_2); 24: while(!feof(fp)) { 25: ch=getc(fp); 26: if(ferror(fp)) f_err_report(ERR_READ); 27: if(!feof(fp)) 28: putc(ch,fp2); 29: else break; 30: if(ferror(fp)) f_err_report(ERR_WRITE); 31: } 32: fclose(fp); 33: fclose(fp2); 34: return 0; 35: } 36:
|
|
posted Dec 1, 2010, 8:25 PM by Neil Mathew
[
updated Dec 1, 2010, 8:48 PM
]
SOURCE CODE:
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
| #include<stdio.h>
int main()
{
int a,b;
printf("\n Enter the value of A: ");
scanf("%d",&a);
printf(" Enter the value of B: ");
scanf("%d",&b);
//Swapping:
a=a+b;
b=a-b;
a=a-b;
printf("\n After Swapping... \n");
printf(" A: %d", a);
printf("\n B: %d", b);
return 0;
} |
OUTPUT:
Enter the value of A: 10
Enter the value of B: 20
After Swapping...
A: 20
B: 10 |
posted Nov 18, 2010, 4:42 AM by Neil Mathew
[
updated Nov 18, 2010, 4:44 AM
]
SOURCE CODE:
1 2 3 4 5 6 7 |
#include<stdio.h> int main() { printf(" Welcome to Neil's Practical File. XD "); return 0; } |
OUTPUT:
Welcome to Neil's Practical File. XD
|
posted Nov 18, 2010, 4:41 AM by Neil Mathew
[
updated Nov 18, 2010, 4:44 AM
]
SOURCE CODE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include<stdio.h> int main() { int i,n,a; printf("\nEnter the number whose table is to be printed : "); scanf("%d",&n); for(i=1;i<11;i++) { a=n*i; printf("%dx%i=%d \n",n,i,a); } return 0; } |
OUTPUT:
Enter the number whose table is to be printed : 5 5x1=5 5x2=10 5x3=15 5x4=20 5x5=25 5x6=30 5x7=35 5x8=40 5x9=45 5x10=50
|
posted Nov 18, 2010, 4:40 AM by Neil Mathew
[
updated Nov 18, 2010, 4:44 AM
]
SOURCE CODE:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <stdio.h> main () { int a,b,c; printf("\n Enter two Numbers: "); scanf("%d%d",&a,&b); c=a ? b:a ; printf("\n Greater number of the two is : %d \n",c); } |
OUTPUT:
Enter two Numbers: 5 357 Greater number of the two is : 357
|
posted Nov 18, 2010, 4:39 AM by Neil Mathew
[
updated Nov 18, 2010, 4:45 AM
]
SOURCE CODE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <stdio.h> main() { char ch; FILE *file; file = fopen("abc.txt", "w"); printf("\nEnter any character into FILE: "); scanf("%c", &ch); putc(ch, file); fclose(file); file = fopen("abc.txt", "r"); ch = getc(file); printf("\nEntered Character from FILE: %c", ch); } |
|
posted Nov 18, 2010, 4:39 AM by Neil Mathew
[
updated Nov 18, 2010, 4:44 AM
]
SOURCE CODE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <stdio.h> main() { char ch; FILE *fp1, *fp2; fp1 = fopen("abc.txt", "r"); fp2 = fopen("xyz.txt", "w"); while((ch = getc(fp1)) != EOF) putc(ch, fp2); fclose(fp1); fclose(fp2); return 0; } |
|
|