Custom Search

Tuesday, 5 April 2011

C /C++ Program to print program to print m characters from string str1 to str2 from back, C /C++ Program to copy m characters from front

C /C++ Program to print program to print m characters from string str1 to str2 from back, C /C++ Program to copy m characters from front


Program
//program to print m characters from string str1 to str2 from front
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 char str1[100], str2[100],n;
 int i,j=0,m;
 clrscr();
 printf("Enter the string\n");
 scanf("%s",str1);
 printf("\nAfter how many caracters you want to copy\n");
 scanf("%d",&m);
 for(i=0;i<m;i++)
 {
  str2[j]=str1[i];
  j++;
 }
 str2[j]='\0';
 printf("New string is:%s",str2);
 getch();
}

Changes for C++
If you want to write the same program in c++ apply the following changes to the program.
1.) change the header file from "#include<stdio.h>" to "#include<iostream>.h" 
2.)  for input & output use "cout<<" & "cin>>" instead of "printf" & "scanf".
3.) don't use "% "symbol and "&" sign in "cin>>" statement.

No comments:

Post a Comment

Laptops