Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ЗФ_ОАиП / Курс Лекций ОАиП.doc
Скачиваний:
65
Добавлен:
21.03.2016
Размер:
5.89 Mб
Скачать

5.5.7 Примеры решения задач

/*Сколько раз слово s1 встречается в строке s? */

#include<stdio.h>

#include<string.h>

#include<conio.h>

#include<ctype.h>

void main()

{

char s[20];

char s1[20];

puts("Введите строку "); gets(s);

puts("Введите слово "); gets(s1);

int k=0,i=0;

int m=strlen(s);

int n=strlen(s1);

printf("m=%d n=%d\n",m,n);

while(i<m)

{

if(strncmp(s1,&s[i],n)==0)

{ if(i==0)

{

if(isspace(s[i+n]) || ispunct(s[i+n]) || (s[i+n]=='\0'))

{ k++; i=i+n; }

else i++;

}

else

if((isspace(s[i-1])||ispunct(s[i-1])) &&

(isspace(s[i+n])|| ispunct(s[i+n]))||(s[i+n]=='\0'))

{ k++; i=i+n; }

else i++;

}

else i++;

}

puts("В строке:");

puts(s);

printf("%d раз(а) встречается %s\n",k,s1);

getch();

}

/*Сколько раз слово s1 встречается в строке s? */

#include<stdio.h>

#include<string.h>

#include<conio.h>

#include<ctype.h>

void main()

{

char s[20];

char s1[20];

puts("Введите строку "); gets(s);

puts("Введите слово "); gets(s1);

int k=0,i=0;

int m=strlen(s);

int n=strlen(s1);

printf("m=%d n=%d\n",m,n);

//Добавление пробела в начало слова

for(i=m;i>=0;i--)

s[i+1]=s[i];

s[0]=' ';

puts(s);

//Поиск в строке

while(i<m+1)

if(strncmp(s1,&s[i],n)==0)

{

if((isspace(s[i+n]) || ispunct(s[i+n]) || (s[i+n]=='\0')) &&

(isspace(s[i-1])||ispunct(s[i-1])))

{ k++; i=i+n; }

else i++;

}

else i++;

//Удаление из строки символа s[0]

for(i=1;i<=m+1;i++)

s[i-1]=s[i];

puts("В строке:");

puts(s);

printf("%d раз(а) встречается %s\n",k,s1);

getch();

}

/*Сколько букв в третьем слове строки s? */

#include<stdio.h>

#include<string.h>

#include<conio.h>

#include<ctype.h>

void main()

{

char s[80];

int k,k3,i,n;

puts("Введите строку "); gets(s);

n=strlen(s);

k=0;//Кол. слов

//Поиск в строке

i=0; //Номер символа

while(i<n && k!=3)

{

if(isalpha(s[i])) //Слово началось

{

k++;

k3=0; //Кол. букв в слове

while(isalpha(s[i]))

{

k3++; i++;

}

}

i++;

}

if(k==3)

{

printf("В третьем слове строки:");

puts(s);

printf(" %d символ(ов,а) \n",k3);

} 2

else {

puts("В строке:");

puts(s);

puts("меньше трех слов");

}

getch();

}

/*Вставить пробел в начало строки (c использ. указателей) */

#include<stdio.h>

#include<string.h>

#include<conio.h>

#include<ctype.h>

void main()

{

char *s,*p;

puts("Введите строку "); gets(s);

int m=strlen(s);

p=s-1;

*p=' ';

puts(s);

puts(p);

s=p;

puts(s);

getch();

}

/*Вставить пробел перед'.' */

#include<stdio.h>

#include<string.h>

#include<conio.h>

#include<ctype.h>

void main()

{

char *s,*p,*c;

puts("Введите строку "); gets(s);

puts(s);

int m=strlen(s);

p=strchr(s,'.');//Позиция .

puts(p);

strncpy(c,s,p-s); //Подстрока до .

puts(c);

strcat(c," ");

strcat(c,p);

s=c;

puts(s);

puts(p);

puts(c);

getch();

}

6 Алгоритмизация и программирование задач, сводящихся к обработке массивов