In C programming, a string is an array of characters terminated by a null character ('\0'). Strings are used to represent sequences of characters such as words, sentences, or entire documents.
char myString[] = "Hello, world!";
#include <stdio.h> int main() { // Define a string variable char brandName[] = "Lit Mentor"; // Print the brand name printf("Welcome to %s!\n", brandName); }
Welcome to Lit Mentor!
#include <stdio.h> int main() { // Define a character array for brand name char brandName[] = {'L', 'i', 't', ' ', 'M', 'e', 'n', 't', 'o', 'r', '\0'}; // Print the brand name printf("Welcome to %s!\n", brandName); return 0; }
Welcome to Lit Mentor!
#include <stdio.h> int main() { // Define a character array for brand name char brandName[11]; // Allocate space for 10 characters plus null terminator // Assign characters one by one brandName[0] = 'L'; brandName[1] = 'i'; brandName[2] = 't'; brandName[3] = ' '; brandName[4] = 'M'; brandName[5] = 'e'; brandName[6] = 'n'; brandName[7] = 't'; brandName[8] = 'o'; brandName[9] = 'r'; brandName[10] = '\0'; // Null terminator to mark end of string // Print the brand name printf("Welcome to %s!\n", brandName); return 0; }
Welcome to Lit Mentor!
#include <stdio.h> int main() { // Define a character array for brand name char brandName[] = "Lit Mentor"; // Print the brand name printf("Welcome to "); // Print each character of the brand name for (int i = 0; brandName[i] != '\0'; i++) { printf("%c", brandName[i]); } // Print a newline character printf("!\n"); return 0; }
Welcome to Lit Mentor!
#include<stdio.h> int main() { char name[] = "Lit Mentor"; int i = 0; while(name[i] != '\0') { printf(" %c", name[i]); i++; } return 0; }
Lit Mentor
#include <stdio.h> int main() { char name[] = "Lit Mentor"; int i = 0; do { printf("%c", name[i]); i++; } while(name[i] != '\0'); return 0; }
Lit Mentor
In below program prompts the user to enter a name, then it reads the input using scanf() and stores it in the str array. However, scanf("%s", str) reads only up to the first whitespace character, so if the user enters a name with a space in it, only the part before the space will be stored in str.
#include <stdio.h> int main() { char str[25]; printf("Enter Name :"); scanf("%s", str); // will scan for string but only one word after space word is not allowed. printf("\n Name = %s", str); // printf is used for formated string printf("\n Name = "); puts(str);// puts() is used for unformated string return 0; }
Enter Name :Ram Singh Name = Ram Name = Ram
#include <stdio.h> int main() { char str[50]; printf("\n Enter a String :"); gets(str); // will read single line as input. [means will stop on enter] printf("\n String is = %s", str); // formated string printf("\n String is = "); puts(str); // puts() is used for unformated string return 0; }
Enter a String :Ram Singh String is = Ram Singh String is = Ram Singh
#include<stdio.h> int main() { char str[25]; // Prompting the user to enter a name printf("Enter Name: "); // Reading the input including spaces using fgets() fgets(str, sizeof(str), stdin); // Printing the entered name using printf() printf("\nName = %s", str); // Printing the entered name using puts() printf("\nName = "); puts(str); return 0; }
Enter Name: Ram Singh Name = Ram Singh Name = Ram Singh
#include<stdio.h> #include<string.h> int main() { char str[]="My Computer Class"; int len; len =strlen(str); printf("\n String = %s \n Length =%d",str,len); return 0; }
String = My Computer Class Length =17
#include<stdio.h> #include<string.h> int main() { char source[] ="My Computer Class"; char target[20]; strcpy(target,source); printf("\n Source String = %s",source); printf("\n Target String = %s",target); return 0; }
Source String = My Computer Class Target String = My Computer Class
#include<stdio.h> #include<string.h> int main() { char name[50]="String "; strcat(name,"Computer "); strcat(name,"Education"); printf("\n %s",name); return 0; }
String Computer Education
#include<stdio.h> #include<string.h> int main() { char str1[25]="String"; char str2[25]="string"; int d=0; d= strcmp(str1,str2); printf("\n Difference=%d",d); if(d==0) { printf("\n Strings are same."); } else { printf("\n Strings are different."); } return 0; }
Difference=-1 Strings are different.
#include<string.h> #include<stdio.h> int main() { char mpass[7]="Ram",npass[7]; int d=0; printf("Enter your Password : \n"); scanf("%s",npass); // d=strcmpi(mpass,npass); // WILL NOT WORK IN LINUX d= strcasecmp(mpass,npass); // ALTERNATE IN LINUX if(d==0) { printf("\n Welcome Password Match."); } else { printf("\n Sorry Password didn't Match."); } return 0; }
Enter your Password : ram Welcome Password Match.
#include<string.h> #include<stdio.h> int main() { char str1[25],str2[25],t[25]; printf("\n Enter Two String : "); scanf("%s %s",str1,str2); printf("\n Strings Before Swapping are :\n"); printf("1. %s \n2. %s",str1,str2); strcpy(t,str1); strcpy(str1,str2); strcpy(str2,t); printf("\n String After Swapping are :\n"); printf("1. %s \n2. %s",str1,str2); return 0; }
Enter Two String : sachin kumar Strings Before Swapping are : 1. sachin 2. kumar String After Swapping are : 1. kumar 2. sachin
#include <string.h> #include <stdio.h> int main() { char str[50]; int i,len; printf("Enter String : \n"); gets(str); for(i=0;str[i]!='\0';i++); len=i; printf("\n Length of String : %d",len); return 0; }
Enter String : Lit Tutorials Length of String : 13
#include <stdio.h> int main() { char str1[25]="Anil Computer Education"; char str2[25]; int i; for(i=0;str1[i]!='\0';i++) { str2[i]=str1[i]; } str2[i]='\0'; printf("\n str1= %s",str1); printf("\n str2= %s",str2); return 0; }
str1= Anil Computer Education str2= Anil Computer Education
#include <stdio.h> int main() { char str1[50]="Anil "; char str2[]="Computer Education"; int i,l; for(i=0;str1[i]!='\0';i++); l=i; for(i=0;str2[i]!='\0';i++) { str1[l+i]=str2[i]; } str1[l+i]='\0'; printf("\n str1= %s",str1); return 0; }
str1= Anil Computer Education
#include<string.h> #include<stdio.h> int main() { char str1[25],str2[25]; int i,j,l; printf("\nEnter a String : "); gets(str1); for(i=0;str1[i]!='\0';i++); l=i; for(i=0,j=l-1;j>=0;j--,i++) { str2[i]=str1[j]; } str2[i]='\0'; puts(str2); return 0; }
Enter a String : Lit Mentor rotneM tiL
#include<stdio.h> int main() { char str[26]; int i,j,len,flag; printf("Enter the string :"); gets(str); for(len=0;str[len]!='\0';len++); flag=1; for(i=0,j=len-1;i<len/2;i++,j--) { if(str[i]!=str[j]) { flag=0; break; } } if(flag) printf("\n It is Palindrome."); else printf("\n It is not a Palindrome"); return 0; }
Enter the string :Lit Mentor It is not a Palindrome
#include <stdio.h> int main() { int i,j,len; char ch,str[50]; printf("Enter a String :"); gets(str); printf("\n Given String = %s",str); for(len=0; str[len]!='\0';len++); for(i=0,j=len-1;i < len/2;i++,j--) { ch=str[i]; str[i]=str[j]; str[j]=ch; } printf("\n Reversed String = %s",str); return 0; }
Enter a String : Lit mentor Given String = Lit mentor Reversed String = rotnem tiL
#include <stdio.h> int main() { int i,j,c=0; char ch[50]; printf("\nEnter String:\n"); gets(ch); for(i=0;ch[i]!='\0';i++) { if((ch[i]=='a') ||(ch[i]=='e') ||(ch[i]=='i') ||(ch[i]=='o') ||(ch[i]=='u') ||(ch[i]=='A') ||(ch[i]=='E') ||(ch[i]=='I') ||(ch[i]=='O') ||(ch[i]=='U')) { c++; } } printf("\nNumber of Vowels =%d",c); return 0; }
Enter String: Lit Mentor Number of Vowels =3
#include <stdio.h> int main() { char str[80]; int v =0,i; printf(" Enter String :"); gets(str); for(i=0;str[i]!='\0';i++) { switch(str[i]) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': ++v; } } printf("\n Number of Vowels =%d ",v); return 0; }
Enter String : Lit Mentor Number of Vowels =3
#include <stdio.h> int main() { char s[58],c; int words=0,lines=0, i=1,j; printf("\nEnter the String and at end press control+z in window and control+d in linux: \n"); while((c=getchar())!=EOF) { s[i] = c; ++i; } /* Insert null character at last */ i=i-1; s[i]='\0'; /* Find number of words and lines */ for(j=1;s[j]!='\0';++j) { if(s[j] == ' ' || s[j] == '\t' || s[j] =='\n') ++words; if(s[j] == '\n') ++lines; } /* Write output */ printf("Number of characters = %d\n",i); printf("Number of words = %d\n",words+1); printf("Number of lines = %d\n",lines+1); }
Enter the String and at end press control+z in window and control+d in linux: Hi This is Lit Mentor online tutorial site just to study for fresher as well as experienced. Number of characters = 139 Number of words = 13 Number of lines = 4
#include <stdio.h> #include <string.h> int main() { char s1[21],s2[21], *rev; printf("\n Enter a String :\n"); gets(s1); strcpy(s2,s1); rev=strrev(s2); if(strcmp(s1,rev) == 0) { printf("String is a palindrome."); } else { printf("String is not a palindrome."); } }
Enter a String : radar String is a palindrome.