Solved Problems Strings
Solved Problems Strings
1.Statement
A word formed from lowercase letters is read. Display the words that are obtained by
the successive removal of the first and last letter from the word read.
Example: If the word alina is read, it will display:
alina
lin
I
Resolution
#include<iostream>
#include<cstring>
int main()
{
char a[100];
cin >> a;
while(strlen(a)>0)
{ cout << a;
cout << endl;
strcpy(a, a + 1);
strcpy(a+strlen(a)-1,a+strlen(a));
}
return 0;
}
2.Statement
A string of characters with a maximum length of 100 characters is read. Let's count and ...
remove the vowels from the string.
Example: For the string abracadabra it displays
5 brcdbr
Resolution
#include<iostream>
#include<cstring>
int main()
{
char a[101], v[]="aeiouAEIOU";
cin.get(a,100);
int i,k=0;
for(i=0;i<strlen(a);i++)
if (strchr(v, a[i]) != 0)
{k++;
strcpy(a+i,a+i+1);
}
cout << k << " " << a;
return 0;
}
3.Statement
A word composed of lowercase letters is read. Each lowercase vowel in the word should be replaced with
corresponding large vocal.
Example: The word algorithm will be transformed into AlgOrItm.
Resolution
#include<iostream>
#include<cstring>
int main()
char v[]="aeiou";
char s[40];
int i;
cin >> s;
for(i=0;i<strlen(s);i++)
if(strchr(v,s[i])!=0) s[i]=s[i]+'A'-'a';
cout << s;
}
4.Statement.
A word is read in the format of a maximum of 200 lowercase letters. All pairs of must be removed.
two identical adjacent letters.
Example: from annaaalina one obtains lina
Resolution
#include<iostream>
#include<cstring>
int main()
char s[200];
int i=0;
cin >> s;
while(i<strlen(s)-1)
if(s[i]==s[i+1])
Copy the substring starting from the current index plus 2 to the current index.
if(i>0) i=i-1;
}
else i++;
}
cout << s;
}
5.Statement.
A text written in lowercase letters and spaces is read. Each word in the text should be replaced.
the first and last letter in the corresponding capital letters.
Example: Ana has apples => AnA hAs ApPleS
Resolution
#include <iostream>
#include <stdlib.h>
6.Statement
Read 2 words a and b. Determine if they are anagrams, displaying yes or no. Two
Words are anagrams if they have the same letters, but in a different order.
Resolution
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
char a[30], b[30];
cin >> a >> b;
if(strlen(a) != strlen(b)) cout << "no";
else
{
int ok=1;
while(strcmp(a,b)!=0 && ok)
{
if(strchr(b,a[0])!=0)
{
strcpy(strchr(b, a[0]), strchr(b, a[0]) + 1);
strcpy(a, a + 1);
}
else ok=0;
}
if(ok) cout << 'yes';
else cout << "no";
}
return 0;
}
7.Statement
A text has at most 100 characters and is made up of words and numbers, separated by spaces.
a space. The words are made up only of letters from the English alphabet. All numbers are
real numbers and are made up only of whole parts or of whole parts and fractional parts,
separate by comma (,), negative numbers being preceded by the minus sign (-).
Write a C/C++ program that reads text from the keyboard, which transforms it by eliminating
its composition all negative numbers. The program then displays the obtained text on the screen.
Example: for the text
-0.2
the text will appear on the screen:
2.7 minus 3.5 minus 2 equals 2.7 plus plus equals result
Solution
#include <iostream>
int main()
char s[100],*p,v[100][100],t[100];
int n,i;
cin.get(s, 100);
n=0;
p=strtok(s," ");
while(p)
{
n++;
strcpy(v[n],p);
p=strtok(NULL," ");
}
strcpy(t, "");
for(i=1;i<=n;i++)
if(strchr(v[i],'-')==0)
strcat(t,v[i]);strcat(t," ");
cout<<t;
return 0;
}
8.Statement
A text with a maximum of 100 characters contains words and numbers, separated by a space.
The words are formed only from lowercase letters of the English alphabet, and the numbers are real.
positive, with the decimal part and the integer part separated by the comma symbol, or only with
the whole part, as in the example. Write a C/C++ program that reads a text from the keyboard
specify the type and display on the screen the number of integer values in the text.
Example: for the text
Grus leucogeranus are 1.40 m in height and lives between 30 and 40 years.
it displays on the screen 2
Resolution
#include <iostream>
#include <cstring>
using namespace std;
int number(char s[100])
int ok=1,i;
for(i=0;i<strlen(s);i++)
if(s[i]<'0' || s[i]>'9') ok=0;
return ok;
}
int main()
{ char s[100],*p;
int k;
cin.get(s, 100); k = 0;
p = strtok(s, " ");
while(p)
{
if(number(p)==1) k++;
p=strtok(NULL," ");
}
cout << k;
return 0; }
9.Statement
In a text with no more than 100 characters, the words are made up of lowercase letters of the alphabet.
English and are separated by a space. Write a C/C++ program that reads from
type a text of the mentioned type and display on the screen, on separate lines, all its words
for which the number of vowels is strictly less than the number of consonants. If there are none
no such word, the message does not exist is displayed on the screen. The letters considered vowels are
the set a, e, i, o, u.
Example: for her text they planted tamarix she brought jasmine
the words planted and tamarix are displayed on the screen, not necessarily in this order
Resolution
#include <iostream>
#include <cstring>
using namespace std;
void verify(char s[250], int &nrv, int &nrc)
{
char voc[250];
int i;
strcpy(voc,"aeiou");
nrv=0;nrc=0;
for(i=0;i<strlen(s);i++)
if(strchr(voc,s[i])!=0) nrv++;
else nrc++;
}
int main()
{
char s[250],*p;
int n1, n2, k;
cin.get(s,250);
k=0;
p=strtok(s," ");
while(p)
{
verific(p,n1,n2);
if(n1<n2) {cout<<p<<endl;
k=1;}
p=strtok(NULL, " ");
}
if(k==0) cout<<"does not exist";
return 0;
}
10.Statement
The file prosir.in is considered, which contains on the first line a text consisting of at most 199 characters.
lowercase letters and spaces. The words in the text are separated by one or more spaces.
The text read from the file should be modified by replacing the last letter of each word with the digit 5.
Example
prosir.in
I have many apples and a quince.
prosir.out
An5 ar5 mult5 mar5 s5 5 gutui5.
Solution
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ifstream f("prosir.in");
ofstream g("prosir.out");
int main()
{
int i;
char s[205];
f.getline(s,205);
for (i=0;i<strlen(s)-1;i++)
if ((s[i]>='a'&&s[i]<='z')&&!(s[i+1]>='a'&&s[i+1]<='z'))
s[i]='5';
11.Statement
A sentence is given formed from uppercase and lowercase letters of the English alphabet, numbers, spaces, and punctuation.
of punctuation, in which capital and lowercase letters are considered identical. Determine the vowel from the string with
maximum number of occurrences. The read string will have a maximum of 255 characters. If the string contains more
multiple vocalizations with the maximum number of occurrences will display the first in alphabetical order.
Example
Ana has 5 apples and three nuts
It is displayed
E
Resolution
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int n, i, j, v[100], p, k, maxim;
char s[255], voc[10], voc2[10];
cin.get(s,255);
strcpy(voc,"aeiou");
strcpy(voc2,"AEIOU");
n = strlen(s);
k = 'a' - 'A';
for (i=0;i<n;i++)
if (strchr(voc,s[i]))
s[i] = s[i] - k;
for (i=0;i<=4;i++)
v[i]=0;
for (i=0;i<n;i++)
if (strchr('AEIOU', s[i]))
{
p = strchr("AEIOU", s[i]) - "AEIOU";
v[p] = v[p] + 1;
}
maxim=0;
for (i=0; i<=4; i++)
if (v[i] > maxim)
maxim=v[i];
for (i=0;i<=4;i++)
if (v[i] == maxim)
cout << voc2[i]; break;
return 0;
}