Lecture 31

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

B.

Tech Batch 2023-24


Lecture 31

Subject Name: Computer Fundamentals and Programming

Subject Code: 2FY208

Strings: Concepts
A String in C programming is a sequence of characters terminated with a null
character ‘\0’. The C String is stored as an array of characters. The difference
between a character array and a C string is that the string in C is terminated with a
unique character ‘\0’.
Declaring a string in C is as simple as declaring a one-dimensional array. Below is the
basic syntax for declaring a string.
char string_name[size];
In the above syntax string_name is any name given to the string variable and size is
used to define the length of the string, i.e the number of characters strings will store.

There is an extra terminating character which is the Null character (‘\0’) used to
indicate the termination of a string that differs strings from normal character
arrays.
A string in C can be initialized in different ways. We will explain this with the help of
an example. Below are the examples to declare a string with the name str and
initialize it with “JIETforStudents”.
We can initialize a C string in 4 different ways which are as follows:
1. Assigning a String Literal without Size
String literals can be assigned without size. Here, the name of the string str acts as a
pointer because it is an array.
char str[] = "JIETforStudent";

2. Assigning a String Literal with a Predefined Size


String literals can be assigned with a predefined size. But we should always account
for one extra space which will be assigned to the null character. If we want to store a
string of size n then we should always declare a string with a size equal to or greater
than n+1.
char str[50] = " JIETforStudent";

3. Assigning Character by Character with Size


We can also assign a string character by character. But we should remember to set
the end character as ‘\0’ which is a null character.
char str[14] = { 'J','I','E','T','f','o','r','S','t','u','d','e','n',’t’,'\
0'};
4. Assigning Character by Character without Size
We can assign character by character without size with the NULL character at the
end. The size of the string is determined by the compiler automatically.
char str[] = { 'J','I','E','T','f','o','r','S','t','u','d','e','n',’t’,'\0'};
Note: When a Sequence of characters enclosed in the double quotation marks is
encountered by the compiler, a null character ‘\0’ is appended at the end of the string
by default.

Read a String Input From the User


#include<stdio.h>
int main()
{
// declaring string
char str[50];
// reading string
scanf("%s",str);
// print string
printf("%s",str);
return 0;
}
Also, you might be thinking that why we have not used the ‘&’ sign with the string
name ‘str’ in scanf statement! To understand this you will have to recall your
knowledge of scanf.
We know that the ‘&’ sign is used to provide the address of the variable to the scanf()
function to store the value read in memory. As str[] is a character array so using str
without braces ‘[‘ and ‘]’ will give the base address of this string. That’s why we have
not used ‘&’ in this case as we are already providing the base address of the string to
scanf.

Note that, the string is read only till the whitespace is encountered.

How to Read a String Separated by Whitespaces in C?


We can use multiple methods to read a string separated by spaces in C. The two of
the common ones are:
We can use the fgets() function to read a line of string and gets() to read characters
from the standard input (stdin) and store them as a C string until a newline character
or the End-of-file (EOF) is reached.
#include <stdio.h>
#define MAX 50
int main()
{
char str[MAX];
// MAX Size if 50 defined
fgets(str, MAX, stdin);
printf("String is: \n");
// Displaying Strings using Puts
puts(str);
return 0;
}
Standard C Library – String.h Functions

The C language comes bundled with <string.h> which contains some useful string-
handling functions. Some of them are as follows:
Function Name Description

strlen(string_name) Returns the length of string name.

strcpy(s1, s2) Copies the contents of string s2 to string s1.

Compares the first string with the second string. If strings are the
strcmp(str1, str2)
same it returns 0.

Concat s1 string with s2 string and the result is stored in the first
strcat(s1, s2)
string.

strlwr() Converts string to lowercase.

strupr() Converts string to uppercase.

strstr(s1, s2) Find the first occurrence of s2 in s1.

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.

o By using the length of string


o By using the null character.
counting the number of vowels in a string.

#include<stdio.h>
void main ()
{
char s[11] = "JIETCOLLEGE";
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 5

counting the number of vowels by using the null character.

#include<stdio.h>
void main ()
{
char s[11] = "JIETCOLLEGE";
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);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy