What is string in c





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.



Here's an example of a string declaration in C:

char myString[] = "Hello, world!";


In this string declaration:

  1. char specifies that each element of the array will be a character.
  2. myString is the name of the array.
  3. "Hello, world!" is the string literal assigned to the array.
  4. The null character '\0' is automatically added at the end of the string literal by the compiler to mark the end of the string.


here's a simple C program that uses strings by litteral and display it.

#include <stdio.h>
int main() {
    // Define a string variable
    char brandName[] = "Lit Mentor";

    // Print the brand name
    printf("Welcome to %s!\n", brandName);
}
Output:
Welcome to Lit Mentor!


You can manually declare a character array and copy each character from the string literal "Lit Mentor" into it. Here's how you can do it

#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;
}
Output:
Welcome to Lit Mentor!  


here's how you can first declare an array and then assign characters one by one:

#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;
}
Output:
Welcome to Lit Mentor!


Here is a program which will print string character by character using for loop.

#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;
}
Output:
Welcome to Lit Mentor!


Here the program prints each character of a string on a next line using while loop.

#include<stdio.h>

int main() {
    char name[] = "Lit Mentor";
    int i = 0;
    while(name[i] != '\0') {
        printf(" %c", name[i]);
        i++;
    }
    return 0;
}
Output:
Lit Mentor


Here the program prints each character of a string on a next line using do while loop.

#include <stdio.h>

int main() {
    char name[] = "Lit Mentor"; 
    int i = 0;
    
    do {
        printf("%c", name[i]);
        i++;
    } while(name[i] != '\0');
    
    return 0;
}
Output:
Lit Mentor


Program to Input and Display String at execution time.


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;
}
Output:
Enter Name :Ram Singh

 Name = Ram
 Name = Ram


2. Program to Input Multiple Words in String (Embedded Blanks) using gets() function.

#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;
}  
Output:
 Enter a String :Ram Singh

 String is = Ram Singh
 String is = Ram Singh


Program to Input Multiple Words in String (Embedded Blanks) using fgets() function.

#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;
}
Output:
Enter Name: Ram Singh

Name = Ram Singh
Name = Ram Singh


Program to find the length of string using strlen() function.

#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;
}
         
Output:
String = My Computer Class
Length =17


Program to copy a string using strcpy() function.

#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;
}
Output:
Source String = My Computer Class
Target String = My Computer Class


7. Program to concat string strcat() function.

#include<stdio.h>
#include<string.h>
int main()
{
    char name[50]="String ";
    strcat(name,"Computer ");
    strcat(name,"Education");
    printf("\n %s",name);
    return 0;
}
Output:
 String Computer Education


8.Program to use strcmp() function to Compare String.

#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;
}
  
Output:
 Difference=-1
 Strings are different.


9.Program to use strcmp() function to compare String.

#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;
}
Output:
 Enter your Password :
 ram
 Welcome Password Match.


Program to swap Two Strings.

#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;
}
Output:
 Enter Two String : sachin
 kumar
 Strings Before Swapping are :
1. sachin
2. kumar
String After Swapping are :
1. kumar
2. sachin


Program to find Length of String without using strlen() function.

#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;
}


Output:
Enter String :
Lit Tutorials

Length of String : 13


Program to copy String without using strcpy() function

#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


Program to concat String without using strcat() function.


#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


Program to Reverse a String without using String function.

#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


Program to check whether the Input String is Palindrome or not.

#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


Program to Reverse a String without using String functions and Second String.

#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


Program to Count the Number of Vowels in the Sentence.

#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


Program to Count the number of vowels in the Sentence using switch.

#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


Write a program to count the number of character, words, and lines in a text.

#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


write a program for string palindrome.

#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.