Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
35
Добавлен:
02.05.2014
Размер:
4.77 Кб
Скачать

#include<stdio.h>
#include<string.h>
#include<conio.h>
void strins1(char*sub, char*s, int p)
{
  if (p>strlen (s))
      p=strlen (s);
  for (int i=strlen (s); i>=p; i--)
    s [i+strlen (sub)]=s[i];
  for (i=0; i<strlen(sub); i++)
    s[i+p]=sub[i];
  return;

}

char* strins3(char*sub, char*s, int p)
  {
     char*begin = new char[strlen(sub)+strlen(s)+1];
	  int k=0;
      for(int i = 0;i<=strlen(s);i++)
      {
	     if(i==p)
		   {for(int j=0;j<strlen(sub);j++)
		     { *(begin+k)=*(sub+j); k++;  }
		    *(begin+k)=*(s+i);k++;
                    }
	  else
		 {*(begin+k)=*(s+i);k++;  }
	  }
       return begin;
  }
void strins2(char*sub, char*s, int p)
{
  if (p>strlen (s))
      p=strlen (s);
  char*ptr=s+strlen(s);
  for (int i=strlen (s); i>=p; i--,ptr--)
    *(ptr+strlen(sub))=*ptr;
  ptr=s+p;
  while(*sub)
    *ptr++=*sub++;
  return;
}

char* strins4(char*sub, char*s, int p)
{
 char*begin = new char[strlen(sub)+strlen(s)+1];

 char*result=begin;
 if(p>strlen(s))
   p=strlen(s);
 for(int i=0; i<p; i++)
     *begin++ =*s++;
 while(*sub)
   *begin++ = *sub++;
 while(*begin++=*s++)
       ;
 return result;
 }


int main(){
     clrscr();

  {char*sub="1234";
  char s[100],s1[100],s3[100],s2[100];
  int p=4;
  strcpy(s,"abcdefghij");
  strcpy(s1,"abcdefghij");
  strcpy(s2,"abcdefghij");
  strcpy(s3,"abcdefghij");
  printf("Test 1: strins1(\"%s\",\"%s\",%d)=",sub,s,p);
  strins1(sub,s,p);
  printf("\"%s\"\n",s);
  printf("Test 1: strins2(\"%s\",\"%s\",%d)=",sub,s1,p);
  strins2(sub,s1,p);
  printf("\"%s\"\n",s1);
  printf("Test 1: strins3(\"%s\",\"%s\",%d)=\"%s\"\n",sub,s2,p,strins3(sub,s2,p));
  printf("Test 1: strins4(\"%s\",\"%s\",%d)=\"%s\"\n",sub,s3,p,strins4(sub,s3,p));
  getch();
  }
     clrscr();
  {char*sub="1234";
  char s[100],s2[100],s3[100],s1[100];
  int p=0;
  strcpy(s,"abcdefghij");
  strcpy(s1,"abcdefghij");
  strcpy(s2,"abcdefghij");
  strcpy(s3,"abcdefghij");
  printf("Test 2: strins1(\"%s\",\"%s\",%d)=",sub,s,p);
  strins1(sub,s,p);
  printf("\"%s\"\n",s);
  printf("Test 2: strins2(\"%s\",\"%s\",%d)=",sub,s1,p);
  strins2(sub,s1,p);
  printf("\"%s\"\n",s1);
  printf("Test 2: strins3(\"%s\",\"%s\",%d)=\"%s\"\n",sub,s2,p,strins3(sub,s2,p));
  printf("Test 2: strins4(\"%s\",\"%s\",%d)=\"%s\"\n",sub,s3,p,strins4(sub,s3,p));

  getch();
  }
     clrscr();
  {char*sub="1234";
  char s[100],s1[100],s2[100],s3[100];
  int p=10;
  strcpy(s,"abcdefghij");
  strcpy(s1,"abcdefghij");
  strcpy(s2,"abcdefghij");
  strcpy(s3,"abcdefghij");
  printf("Test 3: strins1(\"%s\",\"%s\",%d)=",sub,s,p);
  strins1(sub,s,p);
  printf("\"%s\"\n",s);
  printf("Test 3: strins2(\"%s\",\"%s\",%d)=",sub,s1,p);
  strins2(sub,s1,p);
  printf("\"%s\"\n",s1);
  printf("Test 3: strins3(\"%s\",\"%s\",%d)=\"%s\"\n",sub,s2,p,strins3(sub,s2,p));
  printf("Test 3: strins4(\"%s\",\"%s\",%d)=\"%s\"\n",sub,s3,p,strins4(sub,s3,p));

  getch();
  }

     clrscr();
 {char*sub="1234";
  char s[100],s1[100],s2[100],s3[100];
  int p=100;
  strcpy(s,"abcdefghij");
  strcpy(s1,"abcdefghij");
  strcpy(s2,"abcdefghij");
  strcpy(s3,"abcdefghij");
  printf("Test 4: strins1(\"%s\",\"%s\",%d)=",sub,s,p);
  strins1(sub,s,p);
  printf("\"%s\"\n",s);
  printf("Test 4: strins2(\"%s\",\"%s\",%d)=",sub,s1,p);
  strins2(sub,s1,p);
  printf("\"%s\"\n",s1);
  printf("Test 4: strins3(\"%s\",\"%s\",%d)=\"%s\"\n",sub,s2,p,strins4(sub,s2,p));

  printf("Test 4: strins4(\"%s\",\"%s\",%d)=\"%s\"\n",sub,s3,p,strins4(sub,s3,p));
  
  getch();
  }
     clrscr();
 {char*sub="";
  char s[100], s1[100], s2[100],s3[100];
  int p=1;
  strcpy(s,"abcdefghij");
  strcpy(s1,"abcdefghij");
  strcpy(s2,"abcdefghij");
  strcpy(s3,"abcdefghij");
  printf("Test 5: strins1(\"%s\",\"%s\",%d)=",sub,s,p);
  strins1(sub,s,p);
  printf("\"%s\"\n",s);
  printf("Test 5: strins2(\"%s\",\"%s\",%d)=",sub,s1,p);
  strins2(sub,s1,p);
  printf("\"%s\"\n",s1);
  printf("Test 5: strins3(\"%s\",\"%s\",%d)=\"%s\"\n",sub,s2,p,strins3(sub,s2,p));

  printf("Test 5: strins4(\"%s\",\"%s\",%d)=\"%s\"\n",sub,s3,p,strins4(sub,s3,p));

  getch();
  }
     clrscr();
 {char*sub="abcd";
  char s[100], s1[100], s2[100],s3[100];
  int p=1;
  strcpy(s," ");
  strcpy(s1," ");
  strcpy(s2," ");
  strcpy(s3," ");
  printf("Test 6: strins1(\"%s\",\"%s\",%d)=",sub,s,p);
  strins1(sub,s,p);
  printf("\"%s\"\n",s);
  printf("Test 6: strins2(\"%s\",\"%s\",%d)=",sub,s1,p);
  strins2(sub,s1,p);
  printf("\"%s\"\n",s1);
  printf("Test 6: strins3(\"%s\",\"%s\",%d)=\"%s\"\n",sub,s2,p,strins3(sub,s2,p));

  printf("Test 6: strins4(\"%s\",\"%s\",%d)=\"%s\"\n",sub,s3,p,strins4(sub,s3,p));

  getch();
  }
}


Тут вы можете оставить комментарий к выбранному абзацу или сообщить об ошибке.

Оставленные комментарии видны всем.

Соседние файлы в папке Програмки на C++