You are on page 1of 18

string

Introduction
• A string is a sequence of characters.
• It is represented as an array of characters and
the end of the string is marked by the NULL ('\0')
character.
• String constants are enclosed in double quotes.
• String functions are defined in the header file
string.h
 
• Declaration
char a[10];
 
• Initialization
char a[4]={'V', 'I', 'T'};
char a[]={'V', 'I', 'T'};
but, char a[3]= {'V', 'I', 'T'}; is wrong
To read a string or a character array
i) char str[]="Hello";
printf("%s",str);  output: Hello
 
ii) char str[10];
scanf("%s",str);  If HELLO is given as input
printf("%s",str);  output: HELLO
Need not use ‘&’ before ‘s’ in the scanf function,
because the name of the array itself gives the starting
address of the array.
• char str[10];
• scanf("%s",str);  Input: Hello VIT
• printf("%s",str);  output: Hello
scanf function can read the input till it
encounters a space character.
String Handling Functions
• String Length
Syntax: strlen(string);
Returns the length of the string.
• Example
char name[]="James";
printf("%d",strlen(name));  Output: 5
(doesn’t take the ‘\0’ character into account)
String Copy
• Syntax
strcpy(destination_string, source_string);
• Copies the contents of one string to another, i.e the
source string is copied into the destination string.
• Example
char name1[]="James", name2[10];
strcpy(name2,name1)
puts(name2); Output: James
 
String Compare
• Syntax
strcmp(string1, string2);
• Compares two strings and returns an integer, whose
value is
– -1 if the first string is less than the second
– 0 if both are identical
– 1 if the first string is greater than the second
• The strcmp() function compares two strings, character
by character (the ASCII values of characters are
compared).
Example-strrcmp()
#include<stdio.h>
#include<string.h>
main()
{
char line[100], line2[100];
printf("Input first word:");
scanf("%s", line);
printf("Input second word:");
scanf("%s", line2);
if (strcmp(line, line2) == 0)
printf ("equal\n");
if (strcmp(line, line2) < 0)
printf("less then equal\n");
if (strcmp(line, line2) > 0)
printf("greater then equal\n");
}
Strcmp()
OUTPUT
Input first word:ab
Input second word:ab
Equal
----------------------------------------------
Input first word:aa
Input second word:ab
less then equal
--------------------------------------------------
Input first word:ab
Input second word:aa
greater then equal
• strlwr()
• Syntax
strlwr(string);
char s[]= “VIT”;
printf(“%s”,strlwr(s));  Output: vit
Converts a string to lowercase.

• strupr()
• Syntax
strupr(string);
Converts a string to upper-case
 
• strrev()
• Syntax
strrev(string);
Reverses the string.
 
• strchr()
• Syntax
strchr(string,char);
To find the 1st occurrence of a given character in a string.
• strrchr()
• Syntax
strrchr(string,char);
To find the last occurrence of a given character in a string.
 
• strcat()
• Syntax
strcat(destination_string, source_string);
Concatenates the contents of one string to another, i.e the source string is
concatenated to the end of the destination string.
Example-strcpy()andstrcat()
#include<stdio.h>
#include<string.h>
main ()
{
char str[80];
strcpy (str,"one");
strcat (str,"two");
puts (str);
}
OUTPUT
onetwo
strstr() function
• The strstr function locates the first occurrence
of the string string1 in the string string2 and
returns a pointer to the beginning of the first
occurrence.
Example
#include <stdio.h>
int main()
{
char s1 [] = "My House is small";
printf ("String 1: %s\n", strstr (s1, "House"));
}
OUTPUT
String 1: House is small
String Manipulations without using built-in
functions
a) Finding the length of a given string. b) Copying the contents of one string
main() to another.
{ main()
char str1[50]; {
int i; char str1[50],str2[]="University";
puts("Enter a string:"); int i;
gets(str1); for(i=0;str2[i]!='\0';i++)
for(i=0;str1[i]!='\0';i++); str1[i]=str2[i];
printf("The length of the string is str1[i]='\0';
%d\n",i); printf("str1=%s\n",str1);
}
}
Program to sort the strings in
Ascending order
main()
{
char a[20][10],t[10];
int i,j,n;
n=0;
printf("Enter the number of names you want to sort:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",a[i]);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
if(strcmp(a[i],a[j])==1)
{
strcpy(t,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],t);
}
}
printf("Sorted names:\n");
for(i=0;i<n;i++)
printf("%s\n",a[i]);
}

You might also like