Write a program that asks a user to enter the last name of 8 students in a class
ID: 3697691 • Letter: W
Question
Write a program that asks a user to enter the last name of 8 students in a class and save them in a string array. Then use looping statements to sort the names in the array so they are rearranged into an ascending order. Display the names in the array after they are sorted. Write a function that receives a string. The function has no return value, it displays the characters of the string it receives one character per line. Please use following header for the functions: void displayOnePerLine(string a) Write a function that receives a string and a letter. The function calculates and returns the number of occurrence of the letter the function receives. Please use following header for the function: void getFrequenceOf(string a, char c) Write a function that receives a string. The function has no return value, it displays all the letter in the string in a reverse order. Please use following header for the function: void reverse(string a)Explanation / Answer
as you did noot mention the language to write the codes i am writing the codes in c
1.
1.display one per line
#include <iostream>
using namespace std;
#include <cstring>
int main()
{
// declare two arrays named tname with 1-Dimension
// and name with 2-Dimension
char tname[20], name[20][20];
// normal variables...
int i, j, n;
cout<<"Enter the number of names: ";
cin>>n;
// outer loop for counter...
for(i=0; i<n; i++)
{
cout<<" Enter the name(one word) "<<(i+1)<<": ";
cin>>name[i];
}
// inner for loop, read row by row set outer for loop...
for(i=0; i<n-1; i++)
// innermost for loop, read column by column of the characters...
for(j = i+1; j<n; j++)
// set the condition...
// strcmp - compare the string standard library function
// do the sorting...
if(strcmp(name[i], name[j])>0)
{
// strcpy - copy the strings...
// compare and swap...
strcpy(tname, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], tname);
}
cout<<" Sorted names: ";
for (i =0; i<n; i++)
cout<<" "<<name[i];
cout<<endl;
return 0;
}
2. one per line
#include <iostream>
using namespace std;
void displayOnePerLine(string input)
{
cout << "displaying one character per line"<<endl;
for(int i = 0; i < input.length(); i++)
{
cout << input[i]<<endl;
}
}
int main()
{
string input = "";
cout << "Please enter a valid sentence: >";
getline(cin, input);
cout << "You entered: " << input << endl << endl;
displayOnePerLine(input);
return 0;
}
3.frequency of a character
#include<stdlib.h>
#include <iostream>
using namespace std;
void getFrequenceOf(string str,char ch)
{
int count=0,i;
for(i=0; str[i]!=''; i++)
{
if(ch==str[i])
{
count++;
}
}
cout<<"Frequency of the character "<<ch<<" = "<<count;
}
int main()
{
string str = "";
int i, count=0;
char ch;
cout << "Please enter a valid sentence: >";
getline(cin, str);
cout<<"Enter a character to find frequency : ";
cin>>ch;
getFrequenceOf(str,ch);
return 0;
}
4.reverse a string
#include<stdlib.h>
#include <iostream>
using namespace std;
void reverse(string string1)
{
int x;
cout << "Your string backwards: ";
for (x = string1.length()-1; x >=0; x--){
cout << string1[x];
}
}
int main()
{
string str = "";
cout << "Please enter a valid sentence: >";
getline(cin, str);
reverse(str);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.