0% found this document useful (0 votes)
18 views19 pages

17 Strings

Strings in C are sequences of characters represented as arrays of char data types, terminated by a null character ('\0'). Unlike numeric arrays, strings require this termination to identify their end, and various functions exist for string manipulation, including length, copy, concatenation, and comparison. Input and output of strings can be handled using functions like scanf, gets, and printf with the %s format specifier.

Uploaded by

ashutoshraghav01
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)
18 views19 pages

17 Strings

Strings in C are sequences of characters represented as arrays of char data types, terminated by a null character ('\0'). Unlike numeric arrays, strings require this termination to identify their end, and various functions exist for string manipulation, including length, copy, concatenation, and comparison. Input and output of strings can be handled using functions like scanf, gets, and printf with the %s format specifier.

Uploaded by

ashutoshraghav01
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/ 19

Strings

What are Strings


• Strings are sequences of characters that are represented
as arrays of char data types, terminated with a special
character '\0' (called null character).

• Each character in the array occupies one byte of memory


and the last character is always ‘\0’.

• The terminating null (‘\0’) is important, because it is the


only way the functions that work with a string can know
where the string ends.

• In fact, a string not terminated by a ‘\0’ is not really a


string, but merely a collection of characters.
What are Strings
• char name[ ] = { 'H', 'A', 'E', 'S', 'L', 'E', 'R', '\0' } ;

• char name[ ] = "HAESLER" ;

• In the second way of string declaration ‘\0’ is not


necessary. C inserts the null character automatically.
Char Array vs Strings
• A character array is a collection of characters stored
sequentially in memory. It does not have to be null-
terminated.
• E.g. char charArray[5] = {'H', 'e', 'l', 'l', 'o'};

• A string in C is a character array that ends with a special


null character ('\0') to mark the termination of the string.
• E.g. char string[6] = "Hello"; // Automatically includes the
'\0'
Numeric vs non numeric array
• In C/C++, numeric arrays do not have a special
termination character indicating the end of the array. To
process or print all elements, you need to loop through
the array using its size as a reference.

• Character arrays that represent strings in C/C++ are


typically null-terminated (\0), which marks the end of the
string. When you pass the name of a character array to
printf with the %s format specifier, it treats the array as a
string and automatically prints characters until the null-
terminator is encountered. The name of the character
array (e.g., str) acts as a pointer to its first element. The
%s specifier interprets this pointer and starts reading
characters sequentially until it encounters \0.
Accessing the elements of string
#include <stdio.h>
int main()
{
char name[ ] = "Klinsman" ;
int i = 0 ;
while ( i <= 7 )
{
printf ( "%c", name[i] ) ;
i++ ;
}
return 0;
}
Accessing the elements of string
# This program doesn’t rely on the length of the string

#include <stdio.h>
int main()
{
char name[ ] = "Klinsman" ;
int i = 0 ;
while ( name[i]!=’\0’ )
{
printf ( "%c", name[i] ) ;
i++ ;
}
return 0;
}
Accessing the elements of string by Pointer
#include <stdio.h>
int main()
{
char name[ ] = "Anurag“, *ptr ;
ptr=name;
while ( *ptr!='\0')
{
printf ( "%c", *ptr) ;
ptr++ ;
}
return 0;
}
Accessing the elements of string by Pointer
#include <stdio.h>
int main()
{
char name[ ] = "Anurag Jain” ;
printf("%s", name);
return 0;
}

# The %s used in printf( ) is a format specification for


printing out a string.
Receive a string from the keyboard
#include <stdio.h>
int main()
{
char name[25] ;
printf ( "Enter your name " ) ;
scanf ( "%s", name ) ;
printf ( "Hello %s!", name ) ;
return 0;
}

# Note that the declaration char name[25] sets aside 25


bytes under the array name[ ]
# The scanf( ) function fills in the characters typed at
keyboard into this array until the enter key is hit.
# Once enter is hit, scanf( ) places a ‘\0’ in the array.
Receive a string from the keyboard
While entering the string using scanf( ) we must be cautious
about two things:

• The length of the string should not exceed the dimension


of the character array.
• scanf( ) is not capable of receiving multi-word strings.
gets and puts function
#include <stdio.h>
int main()
{
char name[25] ;
printf ( "Enter your name " ) ;
gets(name) ;
puts(name) ;
return 0;
}
gets and puts function
#include <stdio.h>
int main()
{
char name1[25]="sumit", name2[25];
//name2=name1;
//We can't assign a string to another string in C language

char *a="Rohit Modi",*b;


int i=0;
b=a;
//we can assign a char pointer to another char pointer.
while(*(b+i)!='\0')
{
printf("%c",*(b+i));
i++;
}
return 0;
}
Standard Library String Functions
String length
#include <stdio.h>
#include <string.h>

int ustrlen(char *);

int main()
{
char name[25]="sumit";
int l;
l=ustrlen(name);
printf("\nlength of the string by user defined function is: %d",l);
printf("\nlength of the string by pre defined function is: %d",strlen(name));
return 0;
}

int ustrlen(char *p)


{
int i=0;
while(*p!='\0')
{
i++;
p++;
}
return i;
}
String copy
#include <stdio.h>
#include <string.h>

void ustrcpy(char *,char*);

int main()
{
char name1[25]="sumit";
char name2[20], name3[20];
strcpy(name2, name1);
ustrcpy(name3,name2);
printf("\nname1: %s",name1);
printf("\nname2: %s",name2);
printf("\nname3: %s",name3);
return 0;
}

void ustrcpy(char *p, char*q)


{
while(*q!='\0')
{
*p=*q;
q++;
p++;
}
}
String concatenation
#include <stdio.h>
#include <string.h>

void ustrcat(char *,char*);

int main()
{
char name1[40]="manish“, name2[20]="kumar", name3[20]="prateek";
strcat(name1, name2);
ustrcat(name1,name3);
printf("\nname1: %s",name1);
return 0;
}

void ustrcat(char *p, char*q)


{
int i;
i=strlen(p);
p=p+i;
while(*q!='\0')
{
*p=*q;
q++;
p++;
}
}
String comparison
#include <stdio.h>
#include <string.h>

int ustrcmp(char *,char*);

int main()
{
char name1[40]="manish", name2[20]="kumar", name3[20]="prateek";
int i, j;
i=strcmp(name1, name2);
j=ustrcmp(name2,name3);
if(i==0)
printf("\n%s and %s are same", name1, name2);
else
printf("\n%s and %s are different", name1, name2);
if(j==0)
printf("\n%s and %s are same", name2, name3);
else
printf("\n%s and %s are different", name2, name3);
return 0;
}
String comparison
int ustrcmp(char *p, char*q)
{
while(*p!='\0' && *q!='\0' )
{
if(*p==*q)
{
q++;
p++;
}
else
return 1;
}
return 0;
}

You might also like