Prompt the user and read in a string. Make your string large enough to hold 100
ID: 3772330 • Letter: P
Question
Prompt the user and read in a string. Make your string large enough to hold 100 characters. Assuming there is just one blank between each word, count the number of words in the string. Create a new string which is a copy of the original string, except that the new string is converted to uppercase. All non-letter characters should be unchanged. Convert the letters using difference between the ASCII codes for capital letters and the ASCII codes for small letters. Print the original string, the new string, and the word count. Make sure you print messages to label your output. When we call syscall to read in a string, we give a length n in $a1. syscall reads in at most n-1 characters and it adds a null byte (which has the value 0x00) to the end of the string. If the string is less than n-1 characters, then syscall adds a newline (which has the value 0xa) after the null byte. To find the end of the string, test for the null byte. You can ignore the newline when counting words and converting to uppercase. ASCII IN MARS
Explanation / Answer
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char str[100],str2[100];
int len,i,count=0;
cout<<"Enter String: ";
gets(str);
cout<<"String at Start: "<<str<<endl;
for(i=0;i<100;i++)
{
if(str[i]>96&&str[i]<123&&str[i]!=' ')
{
str2[i]=str[i]-32;
}
else if(str[i]==' ')
{
count=count+1;
str2[i]=' ';
}
else
{
str2[i]=str[i];
}
}
cout<<"String after Conversion: "<<str2<<endl;
cout<<"Word Count: "<<count<<endl;
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.