Best Industrial Training in C,C++,PHP,Dot Net,Java in Jalandhar

Tuesday 14 January 2014

C Programs


Concatenation of two strings in c programming language

#include<stdio.h>
int main(){
  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;
}

 

Program to change lowercase to uppercase and vice-versa:

#include<stdio.h>
#include<string.h>
int main(){
  char str[20];
  int i;
  printf("Enter any string:");
  gets(str);
  for(i=0;i<=strlen(str);i++){
            if(str[i]>=97&&str[i]<=122)
            str[i]=str[i]-32;
    else
    str[i]=str[i]+32;
}
    printf("Proper capitalisation is of great importance\n");   
    printf("%s\n",str);
  return 0;
}

OR
#include<stdio.h>
#include<string.h>
int main(){
   char str[20]="abcxyz";
  int i;

  for(i=0;str[i]!='\0';i++){
            if(str[i]>=97&&str[i]<=122)
            str[i]=str[i]-32;
    else
    str[i]=str[i]+32;
}
    cout<<"Proper capitalisation is of great importance\n";  
    cout<<str;
  return 0;
}

Program : Reverse String Without Using Library Function [ Strrev ]

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

void main()
{
 char str[100],temp;
 int i,j=0;

 printf("nEnter the string :");
 gets(str);

 i=0;
 j=strlen(str)-1;

 while(i<j)
     {
     temp=str[i];
     str[i]=str[j];
     str[j]=temp;
     i++;
     j--;
     }

 printf("nReverse string is :%s",str);
 return(0);


No comments:

Post a Comment