Beginners Guide to Strings in C Programming with Codes and Examples

Article by: Manish Methani

Last Updated: November 2, 2021 at 2:04pm IST
3 min 36 sec read

Introduction:

Strings are a fundamental data type in C programming used to store and manipulate text. In this tutorial, you will learn about the basics of strings in C programming with codes and examples.

Declaring Strings:

To declare a string in C programming, you need to define a char array with a size that is large enough to store the string plus one for the null terminator character. For example:

char str[10];

In this example, the size of the char array is 10, which means it can store a string of up to 9 characters plus one for the null terminator.

Initializing Strings:

There are two ways to initialize a string in C programming. You can either initialize it at the time of declaration or assign a value to it later. For example:

char str1[] = "Hello";
char str2[6];
str2[0] = 'W';
str2[1] = 'o';
str2[2] = 'r';
str2[3] = 'l';
str2[4] = 'd';
str2[5] = '';

In the first example, the string "Hello" is assigned to the char array str1 at the time of declaration. In the second example, the string "World" is assigned to the char array str2 by assigning each character individually and adding the null terminator character at the end.

String Input/Output:

To input a string in C programming, you can use the scanf() function with the %s format specifier. For example:

char str[20];
scanf("%s", str);

In this example, the user can input a string of up to 19 characters, which will be stored in the char array str.

To output a string in C programming, you can use the printf() function with the %s format specifier. For example:

char str[] = "Hello, World!";
printf("%s", str);

In this example, the string "Hello, World!" will be outputted to the console.

String Functions: C programming provides various string functions to manipulate strings. Here are some examples:

  • strlen() function: This function returns the length of the string.
char str[] = "Hello";
int len = strlen(str);

In this example, the variable len will contain the value 5.

  • strcpy() function: This function copies the string from the source to the destination.
char src[] = "Hello";
char dest[10];
strcpy(dest, src);

In this example, the string "Hello" is copied from the char array src to the char array dest.

  • strcat() function: This function concatenates two strings.
char str1[] = "Hello, ";
char str2[] = "World!";
strcat(str1, str2);

In this example, the string "World!" is concatenated to the string "Hello, " resulting in the string "Hello, World!".

Conclusion: In this tutorial, you have learned about the basics of strings in C programming with codes and examples. You now know how to declare, initialize, and manipulate strings in C, as well as use string input/output and string functions. By mastering strings, you can unlock the full potential of C programming and create powerful applications.

 

Simplified Coding for Beginners.








Download the Codzify
Mobile App


Mobile App Development, Web App Development, Programming Languages, Latest Tech News & lot more.

Codzify Mobile App

A self-paced learning Courses Created by an Engineer
For Engineers.

Premium

The Complete Angular Course

Instructor: Manish Methani

Explore Curriculum
Free

C Programming for Absolute Beginners

Instructor: Manish Methani

Start Watching
Premium

Flutter Mobile App Development Course

Instructor: Manish Methani

Explore Curriculum
Free

Learn HTML, CSS & Bootstrap

Instructor: Manish Methani

Start Watching

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test
include_once 'codzify-footer.php'; ?>