0% found this document useful (0 votes)
34 views3 pages

C Strings Lect 1-3

Uploaded by

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

C Strings Lect 1-3

Uploaded by

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

C 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, and the last character must always be 0.
There are two ways to declare a string in c language.

• By char array
• By string literal
Let's see the example of declaring string by char array in C language.
1- char ch[10]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
As we know, array index starts from 0, so it will be represented as in the figure given below.

2- char ch[]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};

3- char ch[]="javatpoint";
In such case, '\0' will be appended at the end of the string by the compiler.

DIFFERENCE BETWEEN CHAR ARRAY AND STRING LITERAL


There are two main differences between char array and literal.

• We need to add the null character '\0' at the end of the array by ourself whereas, it is appended internally by
the compiler in the case of the character array.
• The string literal cannot be reassigned to another set of characters whereas, we can reassign the characters
of the array.

String Example in C
#include<stdio.h>
#include <string.h>
int main(){
char ch[11]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
char ch2[11]="javatpoint";

printf("Char Array Value is: %s\n", ch);


printf("String Literal Value is: %s\n", ch2);
return 0;
}

Output

Char Array Value is: javatpoint


String Literal Value is: javatpoint
TRAVERSING STRING
Traversing the string is one of the most important aspects in any of the programming languages. We may
need to manipulate a very large text which can be done by traversing the text. Traversing string is somewhat
different from the traversing an integer array. We need to know the length of the array to traverse an integer
array, whereas we may use the null character in the case of string to identify the end the string and terminate
the loop.
Hence, there are two ways to traverse a string.
• By using the length of string
• By using the null character.

USING THE LENGTH OF STRING


#include<stdio.h>
void main ()
{
char s[11] = "javatpoint";
int i = 0;
int count = 0;
while(i<11)
{
if(s[i]=='a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u' || s[i] == 'o')
{
count ++;
}
i++;
}
printf("The number of vowels %d",count);
}
Output
The number of vowels 4

USING THE NULL CHARACTER


#include<stdio.h>
void main ()
{
char s[11] = "javatpoint";
int i = 0;
int count = 0;
while(s[i] != NULL)
{
if(s[i]=='a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u' || s[i] == 'o')
{
count ++;
}
i++;
}
printf("The number of vowels %d",count);
}
Output
The number of vowels 4
ACCEPTING STRING AS THE INPUT
Till now, we have used scanf to accept the input from the user. However, it can also be used in the case of
strings but with a different scenario. Consider the below code which stores the string while space is
encountered.
#include<stdio.h>
void main ()
{
char s[20];
printf("Enter the string?");
scanf("%s",s);
printf("You entered %s",s);
}
Output
Enter the string?javatpoint is the best
You entered javatpoint
It is clear from the output that, the above code will not work for space separated strings. To make this code
working for the space separated strings, the minor changed required in the scanf function, i.e., instead of
writing scanf("%s",s), we must write: scanf("%[^\n]s",s) which instructs the compiler to store the string s
while the new line (\n) is encountered. Let's consider the following example to store the space-separated
strings.
#include<stdio.h>
void main ()
{
char s[20];
printf("Enter the string?");
scanf("%[^\n]s",s);
printf("You entered %s",s);
}
Output
Enter the string?javatpoint is the best
You entered javatpoint is the best
Here we must also notice that we do not need to use address of (&) operator in scanf to store a string since
string s is an array of characters and the name of the array, i.e., s indicates the base address of the string
(character array) therefore we need not use & with it.

SOME IMPORTANT POINTS


However, there are the following points which must be noticed while entering the strings by using scanf.
• The compiler doesn't perform bounds checking on the character array. Hence, there can be a case where the
length of the string can exceed the dimension of the character array which may always overwrite some
important data.
• Instead of using scanf, we may use gets() which is an inbuilt function defined in a header file string.h. The
gets() is capable of receiving only one string at a time.

You might also like