C++ String Manipulation Problems
C++ String Manipulation Problems
[Link]
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;
}
[Link]
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";
[Link](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;
}
[Link]
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;
}
[Link].
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;
}
[Link].
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>
[Link]
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;
}
[Link]
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;
[Link](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;
}
[Link]
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;
[Link](s, 100); k = 0;
p = strtok(s, " ");
while(p)
{
if(number(p)==1) k++;
p=strtok(NULL," ");
}
cout << k;
return 0; }
[Link]
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;
[Link](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;
}
[Link]
The file [Link] 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
[Link]
I have many apples and a quince.
[Link]
An5 ar5 mult5 mar5 s5 5 gutui5.
Solution
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ifstream f("[Link]");
ofstream g("[Link]");
int main()
{
int i;
char s[205];
[Link](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';
[Link]
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];
[Link](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;
}
The approach involves tokenizing the string to separate words and numbers, checking each token to determine if it is a negative number by looking for a leading '-' character, and constructing a new string excluding these tokens, retaining the rest of the original string content.
Each word in the string should be checked by counting vowels and consonants. If a word has more consonants than vowels, it should be printed. This requires tokenizing the string by spaces, verifying each word by iterating over its characters, and comparing vowel and consonant counts. If no such word is found, a distinct message is displayed.
To count vowels and remove them from a string, iterate through each character in the string, check if it is a vowel using a predefined set of vowels, and increment a counter while removing the vowel by shifting subsequent characters to the left. The final result includes the count of removed vowels and the modified string without vowels.
Two words can be determined to be anagrams by first checking if they are of the same length and then ensuring each character from one word exists in the other. This involves iterating through one string, finding and removing the same character in the second string until all characters are matched. If all characters from one string can be removed from the other, they are anagrams.
To transform the first and last letter of each word in a sentence to uppercase, iterate over the string and detect word boundaries using spaces. Convert the first character of the word and, if applicable, change the last character before a space or end of the string to uppercase.
Convert all vowels in the string to uppercase, then count occurrences of each vowel using an array of counters. Determine the maximum count and identify vowels reaching this count. Sort or compare to find the first alphabetical vowel with this frequency, then display that vowel.
The task involves reading the string and displaying it while iteratively removing the first and last characters. This can be achieved using a loop to continuously extract substrings by skipping the first and last character of the current string until the string becomes empty.
The program should parse the input text for tokens separated by spaces, check each token for digits exclusively without any non-digit character, count them as integers, and print the count. This involves writing a helper function to validate full digit composition of strings.
To replace the last letter of each word in a file with '5', read the string, identify word boundaries using spaces or end-of-line markers, and replace the last letter before each boundary with '5'. Process the string linearly and write the altered string to an output file.
The string can be modified by iterating over it and removing one of the identical adjacent letters whenever a pair is found. This involves copying the portion of the string excluding the duplicate over its current position and potentially adjusting the index to check for new adjacencies created by the removal.