- C Programming
- C Tutorial
- C Basic Syntax
- C Data Types
- C if...else Statement
- C switch Statement
- C for Loop
- C while Loop
- C do...while Loop
- C Jump Statements
- C Arrays
- C Strings
- C Pointers
- C Functions
- C Library Functions
- C Recursion
- C Variable Scope
- C Structures
- C Linked List
- C Stacks
- C Queues
- C Binary Tree
- C Header Files
- C File I/O
- C Programming Examples
- C Programming Examples
C Strings
This post was created and published to provide a description of one of the important topics in the C programming language, which is "string." So without any further delay, let's get started with its brief introduction.
What are strings in C?
A string can be considered a sequence of characters. For example: "fresherearth," "Hello there," "Seattle is a beautiful city," etc. You can also consider the string as an array of characters.
Declare a String in C
Use the following general form to declare a string in the C language:
char name[size];
"char" is the type used to define a character variable; "name" is the name of the string, which is an identifier; and "size" is used to define the number of characters that the character variable "name" can store. For example, consider the following statement:
char str[80];
The above statement tells the compiler that the variable "str" is a character array that can store up to 80 characters.
Note: The null character ('\0') is automatically appended to the end of the string constant by the compiler.
A string constant is a sequence of characters surrounded by double quotes. Here's an illustration.
"Hello there"
Declare and initialize a string in C
To declare and initialize a string in C, use the following general form:
char name[] = "value";
For example,
char str[] = "Hello there";
There is no need to define the "size" if you are going to define the string at the time of declaration. Because the compiler automatically adjusts the size based on the initializing value of the variable.
Use the "%s" format specifier to output a string. For example:
#include<stdio.h> int main() { char myString[] = "fresherearth dot com"; printf("%s", myString); return 0; }
The following snapshot shows the output produced by the above program.
I used the red arrow to draw your attention to the actual output.
You can also use the "%c" format specifier to access the character of the string. Because a string in C is essentially a character array, and as we all know, array indexing always begins with 0, use "mystring[0]" to access the first character of the string and "mystring[strlen(mystring)-1]" to access the last character of the string. Consider the following example as an illustration of this concept:
#include<stdio.h> #include<string.h> int main() { char myString[] = "fresherearth dot com"; printf("The first character = %c", myString[0]); printf("\nThe second character = %c", myString[1]); printf("\nThe third character = %c", myString[2]); printf("\nThe last character = %c", myString[strlen(myString)-1]); printf("\nThe second last character = %c", myString[strlen(myString)-2]); printf("\nThe third last character = %c", myString[strlen(myString)-3]); printf("\n"); return 0; }
The output of this example program demonstrating the access of the desired character in a string is shown in the snapshot given below:
If you want to access a string in a character-by-character manner, then consider the following example:
#include<stdio.h> #include<string.h> int main() { char myString[] = "fresherearth dot com"; for(int i = 0; i < strlen(myString); i++) { printf("%c", myString[i]); } printf("\n"); return 0; }
The output should be:
fresherearth dot com
A string can also be created in the following ways:
#include<stdio.h> #include<string.h> int main() { char str[] = {'H', 'e', 'l', 'l', 'o', ' ', 't', 'h', 'e', 'r', 'e', '\0'}; printf("%s", str); printf("\n"); return 0; }
The output should be:
Hello there
Remember to use the null character ('\0') as the last character of the character array; otherwise, the output console will display unexpected results.
C String Example Programs
The following programs can be considered example programs demonstrating the strings in C:
- Find the length of a given string in C.
- Compare two strings in C.
- Copy a string in C.
- Concatenate the string in C.
- Reverse a string in C.
- Count the number of vowels and consonants in C.
- Delete vowels from a string in C.
- Delete a word from a string in C
- Find the frequency of a character in a string in C.
- Count the number of words in a string.
- In C, remove spaces from a string.
- Sort a string in C.
- uppercase to lowercase in C
- lowercase to uppercase in C
- Swap two strings in C.
- Print the successive and adjacent characters in C.
« Previous Tutorial Next Tutorial »