STRING Q1

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

IF( )

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 

STRING Q2

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

WAP to swap two numbers without using another variable.

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

WAP to display text on screen.

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

 

WAP to display the multiplication table of entered number.

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  

 


WAP to check which of the two entered numbers is greater.

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 

 


WAP to enter and retrieve a character from a file.

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);
}
 

WAP to copy the contents of one file to another.

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

1-10 of 25