Introduction

C style string is a series of characters kept contiguously in memory. C style string is the C++ programming side to deal with letters, numbers, and symbols. In other words, C Style String represents text in computer programming.

C Strings

Strings are a data type in C that represent textual information. They are used to store any text. The most common operations on strings are reading and writing string literals and concatenating two strings.

There are two types of strings: null-terminated and length-prefixed. Null-terminated strings have a single character at the end, which is null, while length-prefixed strings have their size stored before the string itself.

A string is a sequence of characters, like “Hello”. It is a sequence of characters enclosed in double-quotes.

A string can be created by simply enclosing text in double-quotes. For example:”Hello World!””This is an example of a string.”A string can also be created using the C++ String class. For example, to create a new instance of the String class called myString, we can do this:

myString = new String(“Hello World!”);

C Style String 

String in memory

c style string

The C string is a character sequence, which is stored in memory. It can be seen as an array of characters terminated by a null character. The null character is not displayed and has no meaning in the string.

String variables

C programming language provides a simple and efficient way to store text strings.

String variables are the most common variable in the C programming language.

They store text data, which is a sequence of characters. String variables are very useful for storing textual data such as names, addresses, and phone numbers.

Declaring variables

Variables are an excellent way to store data in programs. They allow you to store information that is needed by your program, and they help you organize your code.

The following is a list of variable types that can be declared in C style string format:

  • char – A single character.
  • int – Integer values.
  • long – Long integer values.
  • float – Floating point numbers.
  • double – Double precision floating point numbers.
  • bool – Boolean values (true or false).
  • string – String of characters (null-terminated).

1

char my_last_name = [] {“HAMDI”};

Another format:

2

char my_first_name = [11] {“SAMI”};

The standard C style string functions

There are several most common C style string functions that we list below.

 Functions

strcpy is a C style string function used to copy a string

strcmp is a C style string function used to compare two strings

strcat is a C style string function used to concatenate two strings

strchr is a C style string function used to string scanning operationstrlen is a C style string function used to get the string lengthstrncat is a C style string function used to concatenate one string with part of another

strncmp is a C style string function used to compare parts of two strings

strncpy is a C style string function used to copy part of a string

strrchr is a C style string function used to string scanning operation

Examples

First program

c style string program 1

We require the user to enter his personal data gradually through this program.

The code of the program

#include <iostream>#include <cctype>#include <cstring>using namespace std;int main() {char first_name[20]{};char last_name[20]{};char age[10]{};char email[60]{};cout<<“Please, enter your first name:”<<endl;cin>>first_name;cout<<“Please, enter your last name:”<<endl;cin>> last_name;cout<<“Please, enter your age:”<<endl;cin>> age;cout<<“Please, enter your email:”<<endl;cin>> email;cout<<“Hello, your first name is “<<first_name<<endl;cout<<“Your last name is “<<last_name<<endl;cout<<“Your age is “<<age<<endl;cout<<“Your email is “<<email<<endl;cout <<endl;return 0;}

The output of the program

getline() function example

getline() function program

We ask the user to enter his full name, and then his full name is shown to him.

The code of the program

getline() function output

#include <iostream>#include <cctype>#include <cstring>using namespace std;int main() {char full_name[60]{};cout<<“Please, enter your full name:”<<endl;cin.getline(full_name,50);cout <<endl;return 0;}

The output of the program

 strcat() function example

 strcat() function program

As we said, strncat is a C style string function used to concatenate one string with another.

The code of the program

#include <iostream>#include <cctype>#include <cstring>using namespace std;int main() {char name[15] = {“Sami “};strcat(name, “Hamdi”);cout<<“My name is: “<< name;return 0;}

The output of the program

 strcat() function output

strlen() function example

strlen() function program

We saw that the strlen is a C style string function used to get the string length. In the following program, we want to get the length of two words.

The code of the program

#include <iostream>#include <cctype>#include <cstring>using namespace std;int main() {char first_name[15] = {“Sami”};char last_name[20]={“Hamdi”};cout<<“Length of string first name = “<<strlen(first_name)<<endl;cout<<“Length of string last name = “<<strlen(last_name)<<endl;cout<<endl;return 0;}

The output of the program

strlen() function output

strcmp() function example

strcmp() function

strncmp is a C style string function that compares parts of two strings. In this program, we will compare two words.

The code of the program

#include <iostream>#include <cctype>#include <cstring>using namespace std;int main() {char first_name[15] = {“Sami”};char last_name[15]={“SAMI”};if (strcmp(first_name, last_name) == 0){cout<<“First name and last name match”;}else{cout<<“First name and last name don’t match”;}cout<<endl;return 0;}

The output of the program

strcmp() function output

Conclusion

conclusion of the article

In C, strings are represented as arrays of characters terminated by a null character ”.

The following is an example of a string:

“This is a string.”

The programmer should deal with many C style string functions, such as strcat(), strcmp(), strrchr, and others.

Pin It on Pinterest

Share This