C LAB‎ > ‎(Sem1) Introduction to C‎ > ‎

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
Comments