0% found this document useful (0 votes)
42 views32 pages

C Programming: Strings Overview

Uploaded by

shivam10457
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)
42 views32 pages

C Programming: Strings Overview

Uploaded by

shivam10457
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

Introduction to Strings

Handling of Character 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[10]={'I', 'N', 'D', 'I', 'A', 'G', 'R', 'E', 'A', 'T', '\0'};

As we know, array index starts from 0, so it will be


represented as in the figure given below.

char c[] = "c string";

While declaring string, size is not mandatory. So we can write the


above code as given below:
How to declare a string?
How to initialize strings?
You can initialize strings in a number of ways.
Assigning Values to Strings

Arrays and strings are second-class citizens in C; they do not support the
assignment operator once it is declared. For example,

char c[100];
c = "C programming"; // Error! array type is not assignable.
String Example in C
Let's see a simple example where a string is declared and
being printed. The '%s' is used as a format specifier for the
string in c language.
For Example-
#include <stdio.h> Output
#include <string.h> Char Array Value is: INDIA
String Literal Value is: INDIA
int main()
{
char ch[11] = {'I', 'N', 'D', 'I', 'A', '\0'};
char ch2[11] = "indiagreat";
printf("Char Array Value is: %s\n", ch);
printf("String Literal Value is: %s\n", ch2);
return 0;
}
Read String from the user
You can use the scanf() function to read a string.

The scanf() function reads the sequence of characters until it encounters


whitespace (space, newline, tab, etc.).

Example 1: scanf() to read a string


#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
Scanf and 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.
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.
//using Scanf function
#include <stdio.h> int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
Output:
Enter name: Dennis Ritchie Your name is
Dennis.
//using gets function
#include <stdio.h>
int main(){
char full_name[25];
printf("Enter your full name: ");
gets(full_name);
printf("My full name is %s ",full_name);
return 0;
}
OUTPUT:-
Enter your full name: Dennis Ritchie
My full name is Dennis Ritchie
The fgets() arguments are :
 the string name,
 the number of characters to read,
 stdin means to read from the standard input which is
the keyboard.
Another safer alternative to gets() is fgets() function which
reads a specified number of characters. For example:
#include <stdio.h>
int main()
{
char name[10];
printf("Enter your name plz: ");
fgets(name, 10, stdin);
printf("My name is %s ",name);
return 0;
}
Output:
Enter your name plz:
Carlos My name is Carlos
Fputs() function
The fputs() needs the name of the string and a
pointer to where you want to display the text. We use
stdout which refers to the standard output in order to
print to the screen.
For example:
#include <stdio.h>
int main(){
char town[40];
printf("Enter your town: ");
gets(town);
fputs(town, stdout);
return 0;
}
Output:
Enter your town: New York New York
puts function
The puts function is used to Print string in C on an output device and moving
the cursor back to the first position. A puts function can be used in the
following way
#include <stdio.h>
int main(){
char name[15];
gets(name); //reads a string
puts(name); //displays a string
return 0;
}
The string library
The standard ‘C’ library provides various
functions to manipulate the strings within a
program. These functions are also called as
string handlers. All these handlers are present
inside <string.h> header file.
No Function Description
.
1)
strlen(string_name) returns the length of string name.
2)
strcpy(destination copies the contents of source string
, source) to destination string.
3)
strcat(first_string, concats or joins first string with second string.
second_string) The result of the string is stored in first string.

4)
strcmp(first_string, compares the first string with second string. If
second_string) both strings are same, it returns 0.
5)
strrev(string) returns reverse string.
6)
strlwr(string) returns string characters in lowercase.

7)
strupr(string) returns string characters in uppercase.
C String Length: strlen() function
#include <stdio.h>
#include <string.h>
int main() {
char ch[20] = {'i', 'n', 'd', 'i', 'a', '\0'};
printf("Length of string is: %zu", strlen(ch));
return 0;
}
Output:
Length of string is: 6
The strcpy(destination, source) function copies the source
string in destination
#include<stdio.h>
#include <string.h>
Int main() {
char ch[20] = {'I', 'N', 'D', 'I', 'A', '\0'};
char ch2[20];
strcpy(ch2, ch);
printf("Value of second string is: %s", ch2);
return 0;
}
Output:
Value of second string is: INDIA
C String Concatenation: strcat()
The strcat(first_string, second_string) function concatenates
two strings and result is returned to first_string.
#include<stdio.h>
#include <string.h>
int main()
{
char ch[10] = {'h', 'e', 'l', 'l', 'o', '\0'};
char ch2[10] = {'c', '\0'};
strcat(ch, ch2);
printf("Value of first string is: %s", ch);
return 0;
}
C Compare String: strcmp()
The strcmp(first_string, second_string) function
compares two string and returns 0 if both strings are
equal.
#include<stdio.h>
#include <string.h>

int main(){
char str1[20], str2[20];

printf("Enter 1st string: ");


gets(str1);

printf("Enter 2nd string: ");


gets(str2);

if(strcmp(str1,str2)==0)
printf("Strings are equal");
else
printf("Strings are not equal");

return 0;
}
Output:
Enter 1st string: hello
Enter 2nd string: hello
Strings are equal
//WAP in c to count the length of a string

#include <stdio.h>
int main() {
char str[] = "Hello ,World!";
int length = 0; // Initialize length to 0
int i = 0; // Initialize counter to 0

for (i=0; str[i] != '\0’; i++)


{
length++; // Increment the length

}
printf("The length of the string '%s' is %d\n", str, length);
return 0;
Write a C program to count ‘p’ character available in given string str= “Peoples are
peoPle”.
#include <stdio.h>
int main() {
char str[] = "people are People";
int count = 0;

for (int i = 0; str[i] != '\0'; i++) {


if (str[i] == 'p') {
count++;
}}
printf("The number of 'p' characters in the string is: %d\n", count);
return 0;
}
Thank You..!

You might also like