Sie sind auf Seite 1von 24

STRING

Introduction

string

is

an

array

of

characters

terminated by the NULL character

C implements the string data structure


using arrays of type char.

Important Points about strings

String is always declared as character array.

String literal are enclosed within double quotes,


whereas character literals are enclosed within single
quotes.

Example: A is a string
A is a character

The string is always ended with a null character \0.

The characters after the null character are ignored.

Memory Storage for a String

The C-string

"Hi there!" would be

stored in memory as shown:

! \0

Declaring and Initializing String


Variables
C does not support string as a data type but it can be declared

using character array


Character array declaration syntax :

char identifier[size_specifier]=initialization or
string_literal;

String variables or character array can be initialized in two ways.

By using string literal constant


o

By using initialization list


o

char str[6]=Hello

char str[6]={H,e,l,l,o,\0};

The terminating NULL character is important to determine the


end of a string.

Reading Strings from terminal


Using scanf

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);

Writing strings to terminal


Using printf

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()

converts string to lowercase

10

strncat()

appends a portion of string to another

11

strncpy()

copies given number of characters of one string to another

12

strrchr()

last occurrence of given character in a string is found

13

strrev()

reverses the given string

14

strset()

sets all character in a string to given character

15

strupr()

converts string to uppercase

16

strtok()

tokenizing given string using delimiter

duplicates the string

String Concatenation
Program

Output

#include <stdio.h>

Content of source string = fresh2refresh

#include <string.h>

Content of target string = C Tutorial

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>

Content of source string = fresh2refresh

#include <string.h>

Content of target string = fresh2refresh

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>

First string length = 17

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 ) ;

Second string length = 44

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

char string[55] ="This is a string for testing";

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>

Enter string1: hello

#include<conio.h>

After reversal the string is:

#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);
}

String Upper and Lower


Function
strlwr function

char string[20] =HELLO;


strlwr(string);
puts(string);

strupr function

char string[20] =hello;


strupr(string);
puts(string);

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()

Enter string: hello readers

Enter the character: X

char str[20], ch;

Enter the value of n:

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()

After using strset

ccccccccc

char sre[10]=123456789;
char ch=c;
strset(str,ch);
puts(after using strset);
puts(str);
}

Das könnte Ihnen auch gefallen