Program to write user defined functions for string operations


C File =>


/* Write a program to perform the following operations on strings using user
defined functions:
a. Length of string
b. Copy one string to another
c. Convert string to uppercase*/


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
int main()
{
      char s1[30],s2[30];
      int i,len;

     //Function to calculate length
      int length(char str[30])
     {
         int len = 0;
         while (str[len] != '\0')
         len++;
         return (len);
      }
   // Function to copy string to another
     void copy(char s1[30],char s2[30])
    {
        int i=0;
        for(i=0;i<strlen(s1);i++)
              s2[i]=s1[i];
        printf("\n Copied string = %s",s2);
     }
  // Function to convert string to uppercase
    void case_upr(char s1[30])
   {
       char s[30];
       for(i=0;i<strlen(s1);i++)
         s[i]=toupper(s1[i]);
       s[i]='\0';
       printf("\n String in uppercase : %s",s);
    }
    printf("\n Enter String : ");
    scanf("%s",s1);
    // Calling above defined functions
    printf("\n Length of string = %d",length(s1));
    copy(s1,s2);
    case_upr(s1);
  return 0;
 }

Comments

Popular posts from this blog

Write a program to allocate memory dynamically for n integers. Accept the elements and calculate their sum and average

C program to accept names of n cities and search for city named “Pune”.