C Strings

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

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. The termination character ('\0') is important in a string since it is the
only way to identify where the string ends. When we define a string as char s[10], the character s[10] is implicitly initialized
with the null in the memory.

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.

char ch[5]={'h', 'e', 'l', 'l', 'o', '\0'};

array index starts from 0, so it will be represented as in the figure given below.

0 1 2 3 4 5
h E l l o \0

While declaring string, size is not mandatory. So we can write the above code as given below:

char ch[]={'h', 'e', 'l', 'l', 'o', '\0'};

We can also define the string by the string literal in C language. For example:

char ch[]="hello";

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.

Example:

Program where a string is declared and being printed. The '%s' is used as a format specifier for the string in c language.

#include<stdio.h>

#include<conio.h>

#include <string.h>

int main(){

char ch[5]={'h', 'e', 'l', 'l', 'o', '\0'};

char ch[]="hello";

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

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

getch;}
Output

Char Array Value is: hello

String Literal Value is: hello

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.

There are two ways to traverse a string in C.

 By using the length of string.


 By using the null character.

By using the length of string

Example of counting the number of vowels in a string.

#include<stdio.h>

void main ()

char ch[6]="hello";

int i = 0;

int count = 0;

while(i<6)

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 2


Using the null character

Example of counting the number of vowels by using the null character.

#include<stdio.h>

void main ()

char ch[6]="hello";

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 2

Accepting string as the input: 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? hello this is string

You entered: hellothisisstring

C gets() and puts() functions: The gets() and puts() are declared in the header file stdio.h. Both the functions are
involved in the input/output operations of the strings.

C gets() function: The gets() function enables the user to enter some characters followed by the enter key. All the
characters entered by the user get stored in a character array. The null character is added to the array to make it a
string. The gets() allows the user to enter the space-separated strings. It returns the string entered by the user.

Declaration

char[] gets(char[]);

Reading string using gets()

#include<stdio.h>

void main ()

char s[30];

printf("Enter the string? ");

gets(s);

printf("You entered %s",s);

Output

Enter the string? C is the best

You entered C is the best

C puts() function: The puts() function is very much similar to printf() function. The puts() function is used to print the
string on the console which is previously read by using gets() or scanf() function. The puts() function returns an integer
value representing the number of characters being printed on the console. Since, it prints an additional newline
character with the string, which moves the cursor to the new line on the console, the integer value returned by puts()
will always be equal to the number of characters present in the string plus 1.

Declaration

int puts(char[])
Example to read a string using gets() and print it on the console using puts().

#include<stdio.h>

#include<conio.h>

#include <string.h>

void main()

char name[50];

printf("Enter your name: ");

gets(name); //reads string from user

printf("Your name is: ");

puts(name); //displays string

getch();

Output:

Enter your name: Aman Jaiswal

Your name is: Aman Jaiswal

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