Introduction Data type Variable-Constant Keyword-Identifier Operations Input&Output If-else Switch loops String Array Functions Recursion-Function Mat-Function Pointers File handling Read & write Error handling Read & write error handling

String

Strings are sequences of characters used to store string/text data.

Any group of cheracter defined between bouble quotation(" ") .

C does't support string as a data type


Example
#include <stdio.h>

 int main() {
    // Predefined name & college name
    char name[] = "Vedanth";
    char college[] = "PU University"; 

    // Print the name and college name
    printf("Name: %s\n", name);
    printf("College: %s\n", college);
                    
            return 0;
     }

            

Output
  Name: Vedanth
  College:PU University 

         
Accessing data
In this we access data using the index number[]
   char name[]="Yuvaraja"; 
   // accessing specific element
   printf("%c",name[2]);
        

Example
#include <stdio.h>

 int main() {
  char name[] = "Yuvaraja";
   // Index of the character to access
  int index = 2; 
   
  char character = name[index];
  printf("Character at index %d is :%c\n", index, character);

  printf("Character at index 0 is :%c\n ", name[0]);
     
    return 0;
  }                       
        

Output
 Character at index 0 is: Y
 Character at index 2 is: v
            
Modify / Change the data
Example
#include <stdio.h>
int main() {
  char name[] = "Yuvaraja";
  // Index of the character to access
  int index = 2; 
// Here we modifing the first element 
  name[0]='B';
  char character = name[index];
  printf("Character at index %d is:%c\n", index, character);

  //After change Y to B
  printf(" After modifing the name:%s",name);

  return 0;
}

Output


Character at index 2 is: v
After modifing the name: Buvaraja
 
String length
Finding the total number of string length using the strlen() function

Example
 #include <stdio.h>
 #include <string.h>

 int main() {
   char name[] = "Vedanth";
   int length = strlen(name);

   printf("The length of the string is:%d\n", length);

  return 0;
}

Output
The length of the string is: 7

Coneatentage String
Its comines two string using the strcat() function

Example
#include <stdio.h>

  int main(){
    char first_name[]="Yuva";
    char last_name[]="raja";
    strcat(first_name,last_name);
    printf("Here combines two name :%s",first_name);
    
    return 0;
               }
                

Output
                
Here combines two name :Yuvaraja
Copy string
Here we can copy the one variable value & we can assign to other variable. Using strcpy() function .
Example
#include <stdio.h>

int main(){
    char name[20]="Vedanth";
    char name1[20];
    strcpy(name1,name);
    printf("Copyed after : %s",name1);
    
    return 0;
 }

        

Output
  Copyed after : Vedanth
Compare two string
Here we can compare two string using the strcmp() function.
Example
#include <stdio.h> 
 #include <string.h>
   main()  {  
    // declaration of char array 
   char str1[20]="yuva";   
   char str2[20]="vyuva";   
   int value; 
   // comparing both the strings using strcmp() function  
   value=strcmp(str1,str2);  
   if(value==0)  {
      printf("Strings are same"); 
   } 
   else  {
      printf("Strings are not same");  
}  }
            

Output
 Strings are not same

Open kannada !




@2022, Padma Education.

We cannot warrant full correctness of all content, we try to give our best. While using Padma education website, you agree to have read and accepted our terms and Conditions , privacy policy.