0% found this document useful (0 votes)
12 views

Strings

Uploaded by

abiat2246
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Strings

Uploaded by

abiat2246
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

strings

 The string can be defined as the one-dimensional array of characters


terminated by a null ('\0’).
 The character array or the string is used to manipulate text such as
word or sentences.
 Each character in the array occupies one byte of memory
 The termination character ('\0') is important in a string since it is the
only way to identify where the string ends.
Declaring a string

 The general form


char string_name[size];
 Eg. char s[5];
initialization

 String can be initialized as follows


char s[10]= “Hello”;

H e l l o \0
 char s[ ] = “Hello”;

H e l l o \0
 When the compiler encounters a sequence of characters enclosed in the double
quotation marks, it appends a null character ‘\0’ at the end by default.
 char s[]={‘H’, ’e’, ’l’, ‘l’, ‘o’};

H e l l o \0

 int a[]={1,2,3,4,5};
char name[10]=“Nirmala”;

N i r m a l a \0

int a[10],n
Reading and writing strings

 scanf()
and printf() with the conversion
character %s
 Read
 scanf(“%s”,string_name);
 printff(“%s”,string_name);
 Read a string using gets()
gets(str_name);
 Print a string using puts()
puts(str_name);
 char a[20] =“Nirmala”,b[20];
 b=“MCA”
 B=a
 A>b
 A==b
Operations on string(String handling
functions)
 strlen()
 strcpy()
 strcmp()
 strcat()
 strrev()
 Allthe string handling functions are in
string.h
strlen()

 Calculates the length of a given string.


 This function takes a string as an argument and
returns its length.
 The returned value is of type integer.
 The strlen() function doesn't count the null
character ‘\0’ while calculating the length.
 Eg.
#include<string.h>
int main()
{ int n;
char s[20]=“MCA Programme”;
n=strlen(s);
printf(“Length of the string is %d”,n);
return;
}
strcpy()

 The general form


strcpy(destination, source);
 Thestrcpy() function copies the string
pointed by source (including the null
character) to the destination.
#include<string.h>
int main()
{ char s[20]=“Nirmala College”,str[20];
strcpy(str,s);
printf(“The copied string is %s”,str);
return;
}
void strcopy(char des[],char source[])
{
int i;
for(i=0;source[i]!=‘\0’;i++)
des[i]=source[i];
des[i]=‘\0’;
}
strcmp()

 The strcmp() function compares two strings and returns 0


if both strings are identical.
 The strcmp() function takes two strings and returns an
integer.
 The strcmp() compares two strings character by character.
 If the first character of two strings is equal, the next
character of two strings are compared.
 This continues until the corresponding characters of two
strings are different or a null character '\0' is reached.
 Return Value from strcmp()

Return Value Remarks

0 if both strings are identical (equal)

if the ASCII value of the first unmatched


negative
character is less than the second.

if the ASCII value of the first unmatched


positive integer
character is greater than the second.
int stringcomp(char s1[],char s2[])
{
i=0;
while(s1[i]!='\0'&&s2[i]!='\0')
{
if(s1[i]!=s2[i])
break;
i++;
}
if(s1[i]>s2[i])
return 1;
else if (s1[i]<s2[i])
return -1;
else
return 0;
strcat()

 the strcat() function concatenates (joins) two strings.


 The function definition of strcat() is:
strcat(destination, source);
 The strcat() function concatenates the destination string and
the source string, and the result is stored in the destination
string.
 When we use strcat(), the size of the destination string
should be large enough to store the resultant string. If not,
we will get the segmentation fault error.
 s1=“Nirmala”
 s2=“College” strcat(s1,s2);

N i r m a l a \0 s1

C o l l e g e \0
s2

N i r m a l a C o l l e g e \0
s1
void stringcat(char dest[],char
source[])
{
int i;
for(i=0;dest[i]!=‘\0’;i++);
dest[[i++]=‘ ‘;

for(j=0;source[j]!=‘\0’;j++)
des[i++]=source[j];
dest[i]=‘\0’;
}
strrev
 To reverse a string
 strrev(string);

char s[20]=“MCA @ NIRMALA”;


strrev(s);
printf(“Reverse %s”,s);
 To count vowels and consonants in a line of text
 To count the no. of words in a line of text.
 To reverse a string.
 To check the given string is palindrome or not
 Program to Find the Frequency of given Character in a String
 Pgm to remove all characters except alphabets
 Reverse a string using recursion.

//To count Aphabets, Digits & Sp. Characters
#include <stdio.h>
int main()
{
char s[100];
int i,alpha=0,digit=0,sp=0;
printf("Enter the text:");
gets(s);
for(i=0;s[i]!='\0';i++)
{
if(s[i]>='A'&&s[i]<='Z'||
s[i]>='a'&&s[i]<='z’)
{
alpha++;
}
else if(s[i]>='0'&&s[i]<='9’)
{ printf("No. of Alphabets : %d\n",alpha);
digit++; printf("No. of Digits : %d\n",digit);
} printf("No. of Special Characters : %d\n",sp);
else return 0;
{ }
sp++;
}
Array of strings(2-D char array)

 In order to store a list of names we need a table of string


 char v_name[no.of string][length of string];
 Eg
 char name[5][10];
Here name can store 5 strings each of length 10
 name[0] ->access the first string
 To Access ith string – name[i]
A N U J A \0

A N N M A R Y \0

E L S A \0

S A N G E E T H A \0

S U C H I T H R A \0

You might also like