Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- int len(char *x)
- {
- int i=0;
- while(x[i]!='\0')
- i++;
- return i;
- }
- char * cpy(char *x, char *y)
- {
- int i=0,l=len(y);
- while(i<l)
- {
- x[i]=y[i];
- i++;
- }
- x[i]='\0';
- return x;
- }
- char *cat(char *x, char* y)
- {
- int i=len(x),j=0,l=len(y);
- while(j<l)
- {
- x[i]=y[j];
- i++;
- j++;
- }
- x[i]='\0';
- return x;
- }
- int cmp(char *x, char*y)
- {
- int l1=len(x),l2=len(y),i=0,temp;
- temp=(l1<l2)?l1:l2;
- while(i<temp)
- {
- if(x[i]==y[i])
- ;
- else if(x[i]>y[i])
- return 1;
- else
- return -1;
- i++;
- }
- if(x[i]!='\0')
- return 1;
- else if(y[i]!='\0')
- return -1;
- else
- return 0;
- }
- int cmpi(char *x, char*y)
- {
- int l1=len(x),l2=len(y),i=0,temp;
- temp=(l1<l2)?l1:l2;
- while(i<temp)
- {
- if(x[i]==y[i] || x[i]+32==y[i] || x[i]==y[i]+32)
- ;
- else if(x[i]>y[i])
- return 1;
- else
- return -1;
- i++;
- }
- if(x[i]!='\0')
- return 1;
- else if(y[i]!='\0')
- return -1;
- else
- return 0;
- }
- char *rev(char *x)
- {
- int i=0,j=len(x)-1;
- char temp;
- while(i<len(x)/2)
- {
- temp=x[i];
- x[i]=x[j];
- x[j]=temp;
- i++;
- j--;
- }
- return x;
- }
- char *lwr(char *x)
- {
- int i,l=len(x);
- for(i=0;i<l;i++)
- {
- if(x[i]>=65 && x[i]<=90)
- x[i]+=32;
- }
- return x;
- }
- char *upr(char *x)
- {
- int i,l=len(x);
- for(i=0;i<l;i++)
- {
- if(x[i]>=97 && x[i]<=122)
- x[i]-=32;
- }
- return x;
- }
- void main()
- {
- char str1[100],str2[100];
- printf("STRING MANIPULATION WITHOUT USING LIBRARY FUNCTIONS\n");
- int ch1=0,ch2,flag=0,temp;
- while(ch1<1 || ch1>6)
- {
- if(flag)
- printf("Invalid input!");
- printf("1.Length of string\n2.Copy a string\n3.Concatenate two strings\n4.Compare two strings\n5.Reverse a string\n6.Case conversion\nEnter your choice:");
- scanf("%d",&ch1);
- flag++;
- }
- switch(ch1)
- {
- case 1:
- printf("Enter a string:");
- scanf("%s",&str1);
- printf("Length of %s is %d.",str1,len(str1));
- break;
- case 2:
- printf("Enter source string and target string:");
- scanf("%s %s",&str1,&str2);
- cpy(str1,str2);
- printf("Contents of str2 after copying:%s",str2);
- break;
- case 3:
- printf("Enter two strings:");
- scanf("%s %s",&str1,&str2);
- printf("Before concatenation:\nstr1:%s\tstr2:%s",str1,str2);
- cat(str1,str2);
- printf("\nAfter concatenation:\nstr1:%s\tstr2:%s",str1,str2);
- break;
- case 4:
- flag=0;
- ch2=0;
- while(ch2!= 1 && ch2!=2)
- {
- if(flag)
- printf("Invalid input!\n");
- printf("1.Case sensitive comparison\n2.Case insensitive comparison\nEnter your choice:");
- scanf("%d",&ch2);
- flag++;
- }
- switch(ch2)
- {
- case 1:
- printf("Enter two strings:");
- scanf("%s %s",&str1,&str2);
- temp=cmp(str1,str2);
- printf("Result of comparison:%d\n",temp);
- if(temp==0)
- printf("The strings are equal.");
- else
- printf("The strings are not equal.");
- break;
- case 2:
- printf("Enter two strings:");
- scanf("%s %s",&str1,&str2);
- temp=cmpi(str1,str2);
- printf("Result of comparison:%d\n",temp);
- if(temp==0)
- printf("The strings are equal.");
- else
- printf("The strings are not equal.");
- break;
- }
- break;
- case 5:
- printf("Enter a string:");
- scanf("%s",str1);
- printf("Reverse of %s is ",str1);
- printf("%s",rev(str1));
- break;
- case 6:
- flag=0;
- ch2=0;
- while(ch2!=1 && ch2!=2)
- {
- if(flag)
- printf("Invalid input!\n");
- printf("1.Upper case to lower case\n2.Lower case to upper case\nEnter your chocie:");
- scanf("%d",&ch2);
- flag++;
- }
- switch(ch2)
- {
- case 1:
- printf("Enter the string:");
- scanf("%s",&str1);
- printf("str1 before case-conversion:%s",str1);
- lwr(str1);
- printf("\nstr1 after case-conversion:%s",str1);
- break;
- case 2:
- printf("Enter the string:");
- scanf("%s",&str1);
- printf("str1 before case-conversion:%s",str1);
- upr(str1);
- printf("\nstr1 after case-conversion:%s",str1);
- break;
- }
- break;
- }
- }
Add Comment
Please, Sign In to add comment