STRINGS
- A sequence or series of characters and treated as a
single data item.
- Enclosed with double quotes, it includes letters,
symbols, digits and control characters.
- Declared as char and part of the string.h library
STRING FUNCTIONS
1. strcpy(string1, string2);
-this string function copies the content of string 2 to
string 1.
2. puts(string_var);
-a string output function. This function displays the string
value.
3. strlen(str);
-this function returns the length of the string
4. strcat(str1,str2);
-it appends the string2 to the end of string 1.
5. strlwr(str) ;
-this string function converts all uppercase letters to
lowercase.
6. strupr(str);
--this string function converts all lowercase letters to
uppercase.
7. strrev(str); - reverses all the characters in the string.
8. strcmp(str1, str2); - compares two strings. If string1>string2, the
function returns a positive value; if string 1<string2, the function
returns a negative value. If string 1 == string2, the function
returns a zero value.
9. strcmpi(string1,string2); – same with strcmp() except that
lowercase and uppercase letters are treated equal.
#include <stdio.h>
PROGRAM 1:
#include <string.h>
int main(int argc, char *argv[])
{
char gn[10], fn[10];
strcpy(gn,"Jay");
strcpy(fn,"Roberts");
puts(gn);
puts(fn);
return 0;
}
PROGRAM 2
char gn[10], fn[10];
strcpy(cgn,gn);
char cgn[10], cfn[10];
strcpy(cfn,fn);
printf(“\nEnter your given name: “);
gets(gn);
printf(“\nEnter your family name: “);
gets(fn);
puts(cgn);
puts(cfn);
PROGRAM 3
char str[10];
if (c==0)
int c;
puts("Welcome to the system!!!");
printf(“\nEnter your password: “);
else
gets(str);
puts("Intruder detected!");
c= strcmp(str,"jay1011");
PROGRAM 4
char target[15], source[15];
strcpy(target,"JayRoberts");
strncpy(source,target,7);
puts(source);
PROGRAM 5
#include <stdio.h>
lengthst= strlen(str1);
#include <string.h>
int main()
strcpy(str2,str1);
printf("\n %s",str2);
printf("\n %d",lengthst);
{
return 0;
char str1[20], str2[20];
int lengthst;
printf("\nEnter string 1: ");
gets(str1);
}