Saturday, 5 April 2014

How to Convert an Upper Case String to a Lower Case?

To convert an upper case string to a lower case, I am using arrays along with for loop. This is an easy method you can do it very easily.

 

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
    clrscr();
    char mystring[30];
   int i;

   printf("\nEnter any String in UPPER Case:\n\n");
   scanf("%s",&mystring);

   printf("\nYour Entered String is: %s",mystring); 
   for(i=0;i<=strlen(mystring);i++)
   {
          if(mystring[i]>=97 && mystring[i]<=122)
          mystring[i]=mystring[i]+32;
   }
   printf("\nThe string in LOWER CASE ::\n%s",mystring);
   getch();
}

How to Convert Lower Case String to Upper Case?

To convert a lower case string to an upper case, I am using arrays along with for loop. This is an easy method you can do it very easily.

 

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
    clrscr();
    char mystring[30];
   int i;

   printf("\nEnter any String in Lower Case:\n\n");
   scanf("%s",&mystring);

   printf("\nYour Entered String is: %s",mystring); 
   for(i=0;i<=strlen(mystring);i++)
   {
          if(mystring[i]>=97 && mystring[i]<=122)
          mystring[i]=mystring[i]-32;
   }
   printf("\nThe string in UPPER CASE ::\n%s",mystring);
   getch();
}

How to Concatenate Two Strings with STRCAT

Like numbers you can also concatenate strings or paragraphs. There are several methods for concatenation but I am simplifying two out of all. One is with strcat function and other is by using arrays.

/*Concatenate Two Strings with StrCat Function*/

#include<stdio.h>
#include<conio.h>
main()
{
    clrscr();
    char a[100], b[100];
    printf("\nEnter First String:\n");
    gets(a);
    printf("\nEnter Second String:\n");
   gets(b);

   strcat(a,b);
   printf("\nString After Concatenation:: \n\n%s",a);
  getch();
}

/*Concatenate Two Strings without StrCat*/

 #include<stdio.h>
 #include<string.h>
 #include<conio.h>
int main()
{
  clrscr();
  int i=0,j=0;
  char str1[20],str2[20];
  puts("Enter first string");
  gets(str1);
  puts("Enter second string");
  gets(str2);
  printf("Before concatenation the strings are\n");
  puts(str1);
  puts(str2);
  while(str1[i]!='\0'){
      i++;
  }
  while(str2[j]!='\0'){
      str1[i++]=str2[j++];
  }
  str1[i]='\0';
  printf("After concatenation the strings are\n");
  puts(str1);
 return 0;
}