Answer to Question #271080 in C++ for mod

Question #271080

Write a program which can read your name (separated by a space), and ensures that all the letters are capital (replacing the small letters with capital letters). In the presence of characters that do not belong to the alphabet (ä, ü ... 1, 2, ..., $, #, ...), the program returns an error message. indications: - You declare a string of character size 20: (char string [20];) - Note the difference between (scanf ( "% s", &string);) a (gets (string);) at the reading of a string that contains a space. - You can add the instruction (fflush(stdin);) That can empty the buffer in case of multiple readings. - To go from "a" to "A", we must subtract 32 ASCII (97-32 = 65). - chain [i-1] is the ith character of the string. - A string ends with the character '\0'



1
Expert's answer
2021-11-24T13:47:36-0500
#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;

bool checkString(char arr[])
{
	for (int i = 0; i < strlen(arr); i++)
	{
		if (arr[i] >= 97 && arr[i] <= 122)
		{
			arr[i] = arr[i] - 32;
		}
		else if((arr[i] >= 65 && arr[i] <= 90) || arr[i] == ' '){}
		else
		{
			cout << "Error! Undefined symbol in " << i << " position";
			return false;
		}
	}
	return true;
}

int main()
{
	char string[20];
	do
	{
		fflush(stdin);
		cout << "\nPlease, enter your name (\"quit\"-to escape): ";
		gets_s(string);
		if (strcmp("quit",string)==0)break;
		if (checkString(string))
		{
			cout << "You entered " << string;
		}
		else
		{
			cout << "\nError! Name is invalid! Retry, please!";
		}
	} while (true);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS