Дана строка символов, которая обязательно заканчивается символом точки. Удалить из строки первые буквы каждого слова.
Примечание: написать программу с использованием стандартных функций библиотеки С++.
Ниже приведен код для этой задачи. Код самый простой и может усовершенствоваться (например разбиение его на несколько отдельных функции). В очередной раз мы благодарим Василия Шуверова за его код.
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main ()
{
char a[50000], s[50000];
char b;
int n=0,d=1,i,j=0;
printf("Enter the string and finish it with point:\n\n");
/*ввод предложения */
do
{
b=getchar();
a[n]=b;
++n;
} while (b!= '.');
--n;
printf("\nSo looks the input string without the first letters of each word:\n\n");
/*заполнение массива не начальными буквами каждого слова*/
for (i=0;i<n;++i)
{
if (d)
{
a[i]=0;
d=0;
continue;
}
if (a[i]==' ')
{
d=1;
}
if (a[i] != '0')
{
s[j]=a[i];
++j;
}
}
/*вывод предложения на экран */
for (i=0;i<j;++i)
printf("%c", s[i]);
return 0;
}
Результат работы программы говорит сам за себя:
CppStudio.com
Enter the string and finish it with point: Crystal Method. So looks the input string without the first letters of each word: rystal ethod
Комментарии
YourSpace_tym
// First_letter_of_word.cpp : Defines the entry point for the console application. // created by YourSpace_tym #include "stdafx.h" #include "iostream" #include "string" #include "cctype" using namespace std; void change_string(string& words, int n) { for (int i = n; i < words.length(); i++) { if (i + 1 != words.length()) words[i] = words[i + 1]; else words[i] = ''; } } int main() { string something; cout << "String (end with point '.' ) : "; getline(cin, something); cout << "New string : " << something << endl; for (int i = 0; i < something.length(); i++) { if ((isalpha(something[i]) && (i == 0)) || (isalpha(something[i]) && something[i - 1] == ' ')) { change_string(something, i); while (i + 1 != something.length() && something[i + 1] != ' ') i++; } } cout << "New string : " << something << endl; return 0; }k1llama
#include "stdafx.h" #include <iostream> using namespace std; int main() { char stroka[100]; cin.getline(stroka, 100, '.'); for (int i = 0; i < strlen(stroka); i++) { stroka[i] = stroka[i + 1]; if (stroka[i] == ' ') for (int j = i + 1; j < strlen(stroka); j++) stroka[j] = stroka[j + 1]; } cout << stroka << endl; return 0; }