0% found this document useful (0 votes)
166 views17 pages

Strings in C

Strings in C are arrays of characters terminated by a null character. The document discusses how to initialize, read, and print strings in C. It also covers common string operations like getting the length of a string, copying strings, concatenating strings, comparing strings, and converting case. Functions like strlen(), strcpy(), strcat(), strcmp(), strrev(), strlwr(), and strupr() are described.

Uploaded by

Raminder Cheema
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)
166 views17 pages

Strings in C

Strings in C are arrays of characters terminated by a null character. The document discusses how to initialize, read, and print strings in C. It also covers common string operations like getting the length of a string, copying strings, concatenating strings, comparing strings, and converting case. Functions like strlen(), strcpy(), strcat(), strcmp(), strrev(), strlwr(), and strupr() are described.

Uploaded by

Raminder Cheema
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/ 17

Strings in C

What is String
Strings are actually one-dimensional array of
characters terminated by a null character '\0'. 
Declaration of Character:
char a;
Declaration of String:
char myString[10];
The characters after the null character are ignored.
'C' language does not directly support string as a data
type. Hence, to display a string in 'C', you need to
make use of a character array.
String Initialization
Initialization Syntax:
- char str [] = { 'H','A','E','S', 'L', 'E', 'R', '\0' } ;
- char myString[10] = “Hello”;
- char myString[] = “Initial value”;
where \0 = null character, denotes end of the string.

Note: that ‘\0’ and ‘0’ are not same.


Reading string from user
Using scanf()
Using scanf, a string can be read using %s format specifier.
Syntax:
char str[30];
scanf(“%s”, mystring); //& is optional with string

Problem with scanf():


Can read only single word.
terminates its input on the first white space it finds.
white space includes blanks, tabs, carriage returns(CR), form
feeds & new line.
Reading string from user
Using scanf()
Example:
char str[30];
printf(“Enter string:”);
scanf(“%s”,str);
printf(“String is:%s”,str);

OUTPUT:
Enter string:Hello World
String is:Hello
Reading string from user
Using gets()
Takes a string from standard input and assigns it to a
character array.
Can read multi word strings.
Syntax:
Example:
char str[30]; char str[30];
printf(“Enter string:”);
gets(str); gets(str);
printf(“String is:%s”,str);

OUTPUT:
Enter string:Hello World
String is:Hello World
Reading string from user
Using getchar()
getchar() as the name states reads only one character at
a time.
In order to read a string, we have to use this function
repeatedly until a terminating character is encountered.
The characters scanned one after the other have to be
stored simultaneously into the character array.
Syntax: for single character
char str;
str=getchar();
Reading string from user
Using getchar()
int main()
{
char str[50], ch; int i;
printf("Enter a string: ");
i = 0;
ch = getchar ();
while(ch!='\n')
{
str[i] = ch;
i++;
ch = getchar();
}
str[i] ='\0';
printf("Entered string is: %s", str);
return 0;
}
Printing strings
Using printf()
Using printf, a string can be displayed using %s format
specifier.
char str[10]=“Hello”;
printf(“%s”,str);
Using puts()
It just takes its parameter as the string to be printed.
char str[20]=“Hello World”;
puts(str);
Printing strings
Using putchar()
putchar() as the name states prints only one character at a
time.
In order to print a string, we have to use this function
repeatedly until a terminating character is encountered.
i = 0;
while(str[i]!='\0')
{
putchar( str[i] );
i++;
}
String Opertions-string.h
strlen()
returns number of characters in string
strcpy()
copy one string into another
strcat()
append one string onto the right side of the other
strcmp()
compare alphabetic order of two strings
strrev()
reverse the given string
strlwr()
converts the given string to lowercase
strupr()
converts the given string to uppercase
strlen()
The strlen() function takes a string as an argument and
returns its length.
Example:
char a[20]="Program";
printf("Length of string a = %ld \n",strlen(a));
strcpy()
strcpy(target, source)
Copies source string into target string
Example:
char str1[10]= "awesome";
char str2[10];
strcpy(str2, str1);
puts(str1);
puts(str2);
strcat()
The function strcat() concatenates two strings.
Example:
char str1[] = "This is ", str2[] = "programiz.com";
//concatenates str1 and str2 and resultant string is stored in
str1. strcat(str1,str2);
puts(str1);
puts(str2);
strcmp()
The strcmp() compares two strings character by character and
returns an integer.
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 is 0 if both strings are identical
Return value is negative if the ASCII value of the first unmatched
character is less than second.
Return value is positive integer if the ASCII value of the first
unmatched character is greater than second.
strcmp()
Example:
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result;
// comparing strings str1 and str2
result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result);
// comparing strings str1 and str3
result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %d\n", result);
strrev()
Char str[10]=“Hello”;
strrev(str);
puts(str);
strlwr()
strlwr(str);
strupr()
strupr(str)

You might also like