![]() |
| String in C Programming | Complete Beginner’s Guide with Examples |
📑 Table of Contents
▼
C String Definition
String: A strings are represented as arrays of characters terminated by a null character ('\0'). C doesn't have a built-in string data type so we work with character arrays to handle textual data.
String is a collection of character. C does not support string data types therefore char data type is used to make a string.
String in C is stored in single dimension character array. There are many predefined string function in C library. String predefined function include string.h header file.
String Functions in C Programming
| String Function Name | Descriptions |
|---|---|
| strlen(s); | It is used to get the length of string. |
| strcpy(s1,s2); | It is used to copy the string. Here String s2 is copied into string s1. |
| strcat(s1,s2); | It is used to add two string. Here string s2 marge at the end of string s1. |
| strcmp(s1,s2); | It is used to compare two string. This function returns 0 if string s1 and s2 are same. |
| strrev(s); | It is used to reverse the string character. |
| strupr(s); | It is used to convert all the character into upper alphabet. |
| strlwr(s); | It is used to convert all the character into lower alphabet. |
String Length: String length in C is the count of characters stored in a character array that represents a string, excluding the terminating null character ('\0').
String Length in C Example
| #include<stdio.h> void main(){ clrscr(); char s[10]="Jiocoding"; printf("String Character Length:%d",strlen(s)); getch(); } ============OUTPUT============ String Character Length:9 |
String Copy Function in C
String Copy: String copy in C is the process of transferring all characters from one string to another string.
String Copy Function in C Example
| #include<stdio.h> void main(){ clrscr(); char s1[10]="Jiocoding"; char s2[10]="Website"; printf("String Copy Function:%s",strcpy(s1,s2)); getch(); } ============OUTPUT============ String Copy Function:Website |
String Concatenation in C
String Concatenate: String concatenation in C is the process of joining two strings by adding the characters of one string to the end of another string, including the null terminator at the end of the final combined string.Example of String Concatenate in C
| #include<stdio.h> void main(){ clrscr(); char s1[100]="Jiocoding "; char s2[10]="Webste"; printf("String Concatenate Function:%s",strcat(s1,s2)); getch(); } ============OUTPUT============ String Concatenate Function:Jiocoding Webste |
String Compare Function in C
String Compare: String comparison in C is the process of checking two strings character by character to determine whether they are equal or which one is greater or smaller based on their ASCII values.String Compare Example in C
| #include<stdio.h> void main(){ clrscr(); char s1[10]="Jiocoding"; char s2[10]="Jiocoding"; printf("String Compare Function\n"); if(strcmp(s1,s2)==0){ printf("String are same"); } else{ printf("String are not same"); } getch(); } ============OUTPUT============ String Compare Function String are same |
String Reverse Function in C Language
String Reverse: String reverse in C is the process of changing the order of characters in a string so that the first character becomes the last, the second becomes the second last, and so on, while keeping the null terminator ('\0') at the end.String Reverse Function in C Program
| #include<stdio.h> void main(){ clrscr(); char s[100]="Jiocoding"; printf("String Reverse Function:%s",strrev(s)); getch(); } ============OUTPUT============ String Reverse Function:gnidocoiJ |
String Uppercase Function in C
String Upper: String uppercase in C is the process of converting all lowercase letters in a string into their corresponding uppercase letters.Example of String Upper in C
| #include<stdio.h> void main(){ clrscr(); char s[100]="Jiocoding"; printf("String Upper Function:%s",strupr(s)); getch(); } ============OUTPUT============ String Upper Function:JIOCODING |
String Lowercase Function in C
String Lower: String lowercase in C is the process of converting all uppercase letters in a string into their corresponding lowercase letters.
Example of String Lower in C
| #include<stdio.h> void main(){ clrscr(); char s[100]="JIOCODING"; printf("String Lower Function:%s",strlwr(s)); getch(); } ============OUTPUT============ String Lower Function:jiocoding |
How to Find String Length without using inbuilt function in C
void main(){ clrscr(); char s[100]; int c=0; printf("Enter any String:"); gets(s); for(int i=0;s[i]!='\0';i++) { c++; } printf("Length of String:%d",c); getch(); } ============OUTPUT============ Enter any String:jiocoding Length of String:9 |
How to Create Login Program Using String in C
| #include<stdio.h> void main(){ clrscr(); char username[100]="jiocoding"; char password[100]="1234"; char user[100]; char pwd[100]; printf("Enter Username:"); scanf("%s",&user); printf("Enter Password:"); scanf("%s",&pwd); if(strcmp(username,user)==0&&strcmp(password,pwd)==0) { printf("Login Successfully..."); } else { printf("Username or password invalid..."); } getch(); } ============OUTPUT============ Enter Username:jiocoding Enter Password:1234 Login Successfully... |
String Palindrome Program in C
| #include<stdio.h> void main(){ clrscr(); char s[100]; char p[100]; printf("Enter any String:"); gets(s); strcpy(p,s); strrev(p); if(strcmp(s,p)==0) { printf("String is Palindome:%s",s); } else { printf("String is not Palindome:%s",s); } getch(); } ============OUTPUT============ Enter any String:madam String is Palindome:madam |
Reverse String without using inbuilt function in C
| #include<stdio.h> void main(){ clrscr(); char s[100]; char s[100]="JIOCODING"; int rev=0; printf("Enter any String:"); gets(s); printf("Before Reverse String:%s\n",s); for(int i=0;s[i]!='\0';i++) rev++; printf("After Reverse String\n"); for(int i=rev-1;i>=0;i--) printf("%c",s[i]); getch(); } ============OUTPUT============ Enter any String:jiocoding Before Reverse String:jiocoding After Reverse String gnidocoij |
How to Count Only Alphabets in a String
| #include<stdio.h> void main(){ clrscr(); char s[100]; int alpha=0; printf("Enter any String:"); gets(s); for(int i=0;s[i]!='\0';i++) { if((s[i]>=65&&s[i]<=90)||(s[i]>=97&&s[i]<=122)) alpha++; } printf("Total Alphabets:%d",alpha); getch(); } ============OUTPUT============ Enter any String:jiocoding@gmail.com Total Alphabets:17 |
How to Count Only Digit in a String
| #include<stdio.h> void main(){ clrscr(); char s[100]; int digit=0; printf("Enter any String:"); gets(s); for(int i=0;s[i]!='\0';i++) { if((s[i]>=48&&s[i]<=57)) digit++; } printf("Total Digit:%d",digit); getch(); } ============OUTPUT============ Enter any String:jiocoding123@gmail.com Total Digit:3 |
How to Count Only Special Symbol in a String
| #include<stdio.h> void main(){ clrscr(); char s[100]; int symbol=0; printf("Enter any String:"); gets(s); for(int i=0;s[i]!='\0';i++) { if((s[i]>=65&&s[i]<=90)||(s[i]>=97&&s[i]<=122)) { } else if(s[i]>=48&&s[i]<=57) { } else symbol++; } printf("Total Special Symbols:%d",symbol); getch(); } ============OUTPUT============ Enter any String:jicoding@gmail.com Total Special Symbols:2 |
How to Count Total Space in a String
| #include<stdio.h> void main(){ clrscr(); char s[100]; int space=0; printf("Enter any String:"); gets(s); for(int i=0;s[i]!='\0';i++) { if(s[i]==' ') space++; } printf("Total Space in a String:%d",space); getch(); } ============OUTPUT============ Enter any String:welcome to jiocoding website Total Space in a String:3 |
How to Count Total Vowel and Consonant in a String
| #include<stdio.h> void main(){ clrscr(); char s[100]; int v=0; int c=0; printf("Enter any String:"); gets(s); for(int i=0;s[i]!='\0';i++) { if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U') v++; else c++; } printf("Total Vowel:%d\n",v); printf("Total Consonent:%d",c); getch(); } ============OUTPUT============ Enter any String:jicoding Total Vowel:3 Total Consonent:5 |

0 Comments