Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • String function in C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 68
    Comment on it

    While working with character and strings we need to use predefined string type functions for making our task easy and efficient.

     

    Function Work of Function
    strlen() Calculates the length of string
    strcpy() Copies a string to another string
    strcat() Concatenates(joins) two strings
    strcmp() Compares two string
    strlwr() Converts string to lowercase
    strupr() Converts string to uppercase

     

    Strlen function is used to find the length of the string.

    #include <stdio.h>
    #include <string.h>
    int main(){
        char a[20]="Program";
        char b[20]={'P','r','o','g','r','a','m','\0'};
        char c[20];
        printf("Enter string: ");
        gets(c);  
        printf("Length of string a=%d \n",strlen(a));
        //calculates the length of string before null charcter.
        printf("Length of string b=%d \n",strlen(b));   
        printf("Length of string c=%d \n",strlen(c));
        return 0;
    }

     

    Strcpy function is used to copy the string from source to destination

    #include <stdio.h>
    #include <string.h>
    int main(){
        char a[10],b[10];
        printf("Enter string: ");
        gets(a);
        strcpy(b,a);   //Content of string a is copied to string b.
        printf("Copied string: ");
        puts(b);
        return 0;
    }

     

    Strcat function is used to concatenate string.

    #include <stdio.h>
    #include <string.h>
    int main(){
        char str1[]="This is ", str2[]="demo program";
        strcat(str1,str2);   //concatenates str1 and str2 and resultant string is stored in str1.
        puts(str1);    
        puts(str2); 
        return 0;
    }

    getc() and putc()

    For taking the input as character type and displaying it as an character type these functions are used

     

    #include<stdio.h>
    int main(){
        char name[30];
        printf("Enter name: ");
    for(int i=0;i<=29;i++)
    {
        getc(name[i]);     //Function to read string from user.
    } 
    
    for(int i=0;i<=29;i++)
    {
       printf("Name: ");
        puts(name[i]);      //Function to display string.
    }  
        return 0;
    }

     

    gets() and puts()

     

    For taking entire string as input or displaying it without using an array you can use gets and puts for it.

     

    #include<stdio.h>
    int main(){
        char name[30];
        printf("Enter name: ");
        gets(name);     //Function to read string from user.
        printf("Name: ");
        puts(name);    //Function to display string.
        return 0;
    }

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: