Beruflich Dokumente
Kultur Dokumente
Introduction
string
is
an
array
of
characters
Example: A is a string
A is a character
The C-string
! \0
char identifier[size_specifier]=initialization or
string_literal;
char str[6]=Hello
char str[6]={H,e,l,l,o,\0};
char address[10];
function
scanf(%saddress);
Reading a line of
char line[80];
text
scanf(%[^\n],line);
printf(%s,line);
Using
char ch;
getchar()function
ch=getchar();
Using
char letter;
gets()function
gets(letter);
printf(%s,addr
function
Using putchar
ess);
char ch=A;
function
Using puts
putchar(ch);
puts(letter);
function
STRING HANDLING
FUNCTIONS
S.no
1
2
3
4
5
6
7
Array
declaration
strcat (str1,
str2)
strcpy (str1, str
2)
strlen (strl)
strcmp (str1,
str2)
strchr (str1,
char)
strstr (str1,
str2)
strcmpi (str1,
Array initialization
Concatenates str2 at the end of str1.
Copies str2 into str1
Gives the length of str1.
Returns 0 if str1 is same as str2.Returns <0 if strl < str2.Returns >0 if
str1 > str2.
Returns pointer to first occurrence of char in str1.
Returns pointer to first occurrence of str2 in str1.
Same as strcmp() function. But, this function negotiates case. A and
a are treated as same.
str2)
strdup()
strlwr()
10
strncat()
11
strncpy()
12
strrchr()
13
strrev()
14
strset()
15
strupr()
16
strtok()
String Concatenation
Program
Output
#include <stdio.h>
#include <string.h>
int main( )
{
char source[ ] = " fresh2refresh" ;
char target[20]= " C Tutorial" ;
strcat ( target, source ) ;
printf ( "\Content of source string =
%s", source ) ;
printf ( "\Content of target string = %s",
target ) ;
}
fresh2refresh
String Copy
Program
Output
#include <stdio.h>
#include <string.h>
int main( )
{
char source[ ] = "fresh2refresh" ;
char target[20] ;
strcpy ( target, source ) ;
printf ( "\nContent of source string =
%s", source ) ;
printf ( "\nContent of target string = %s",
target ) ;
}
Stringn copy
Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[15], str2[15];
int n;
clrscr();
printf(enter source string);
gets(str1);
printf(enter destination string);
gets(str2);
printf(enter number of characters to replace in
destination string):
scanf(%d,&n);
strncpy(str2,str1,n);
printf(Source string=%s,str1);
printf(\n destination string=%s, str2);
}
Output
Enter
source
string:
engineering
Enter
destination
string:
technology
Enter number of characters to
replace in destination string:
6
Source string: engineering
Destination string: enginelogy
String Length
#include <stdio.h>
Output
#include <string.h>
int main( )
{
int len1, len2 ;
char array1[20]="fresh2refresh.com" ;
char array2[60]="fresh2refresh.com
value
assigned " \\ "at run time";
len1 = strlen(array1) ;
len2 = strlen(array2) ;
printf ( "\nFirst string length = %d \n" ,
len1 ) ;
printf ( "\nSecond string length =
%d \n" , len2 ) ;
String Compare
#include <stdio.h>
Output
#include <string.h>
0 -1 1
int main( )
{
char str1[ ] = "fresh" ;
char str2[ ] = "refresh" ;
int i, j, k ;
i = strcmp ( str1, "fresh" ) ;
j = strcmp ( str1, str2 ) ;
k = strcmp ( str1, "f" ) ;
printf ( "\n%d %d %d", i, j, k ) ;
}
String Character
#include <stdio.h>
Output
#include <string.h>
Character i found at 3
int main ()
Character i found at 6
Character i found at 14
Character i found at 26
char *p;
p = strchr (string,'i');
while (p!=NULL)
{
printf ("Character i found at %d\n",pstring+1);
p=strchr(p+1,'i');
}
return 0;
}
String Compare
Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[15], str2[15];
int result;
clrscr();
printf(enter string1);
gets(str1);
printf(enter string2);
gets(str2);
result=strcmp(str1,str2);
if(result==0)
puts(strings are equal);
else
puts(strings are not equal);
}
Output 1
enter string1: hello
Enter string2: HELLO
Strings are not equal
Output 2
Enter string1:hello
Enter string2: hello
Strings are equal
strcmpi function
Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[15], str2[15];
int result;
clrscr();
printf(enter string1);
gets(str1);
printf(enter string2);
gets(str2);
result=strcmpi(str1,str2);
if(result==0)
puts(strings are equal);
else
puts(strings are not equal);
}
Output
enter string1: hello
Enter string2: HELLO
Strings are equal
strncmp function
Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[15], str2[15];
int result, n;
clrscr();
printf(enter string1);
gets(str1);
printf(enter string2);
gets(str2);
printf(enter the value of n);
scanf(%d, &n);
result=strncmp(str1,str2,n);
if(res==0)
puts(strings portions are equal);
else
puts(strings portions are not
equal);
}
Output
enter string1: hello
Enter string2: Hai
Enter the value of n:1
Strings are equal
strrev function
Program
Output
#include<stdio.h>
#include<conio.h>
#include<string.h>
Olleh
void main()
{
char str1[15];
clrscr();
printf(enter string1);
gets(str1);
strrev(str1);
printf(after reversal the string is);
puts(str1);
}
strupr function
strncat function
Program
#include<stdio.h>
#include<string.h>
main()
{
char dest[5], src[5];
int n;
puts(enter strings:);
gets(dest);
gets(src);
puts(enter the value of n:);
scnaf(%d,&n);
strncat(dest,src,n);
puts(after concatenation);
puts(dest);
}
Output
Enter string:
Hello
Readers
Enter the value of n: 7
After concatenation:
helloreaders
strnset function
Program
Output
main()
int n;
puts(enter string);
XXXXXXreaders
gets(str);
printf(enter character);
scanf(%c,&ch);
printf(enter the value of n);
scanf(%d, &n);
strnset(str,ch,n);
puts(str);
}
strset function
Program
Output
main()
ccccccccc
char sre[10]=123456789;
char ch=c;
strset(str,ch);
puts(after using strset);
puts(str);
}