Hi, we having this project in C, and as far as the course goes with are about to
ID: 3664507 • Letter: H
Question
Hi, we having this project in C, and as far as the course goes with are about to start with arrays, I found a code solution for the project but have some doubts in the steps as they haven't been covered in class. Could you help commenting the steps in the code, specially this line: for(i--;i<100;i++) strcpy(&line[i], null);, how come is decrementing and incrementing i at the same time, is it allowe for a loop sytax begin with i--? Thanks.
Write a program that takes a message entered by the user, remove all punctuation (nonletters) except white spaces, bring all letters to uppercase, and then print the message. For example, with the input
the output text should be
1) Name your program remove.c.
2) Assume the input only contains punctuation and letters and does not contain digits.
3) The user input ends with the user pressing the enter key (a new line character).
4) You can use character handling functions such as toupper and isalpha. Don’t forget to
include ctype.h if you use any character handling functions. the code is as follows.
#include
#include
#include
int main(){
int i=0;
char line[100], c='a';
const char* null = "";
printf("Enter a line: ");
while(c!=' '){
c = getchar();
line[i] = c;
i++;
}
for(i--;i<100;i++) strcpy(&line[i], null);
for(i=0; i<100; i++){
if(isalpha(line[i])) line[i] = toupper(line[i]);
else if(line[i]!= ' ') strcpy(&line[i], null);
}
printf("Modified line: ");
for(i=0; i<100; i++){
printf("%c", line[i]);
}
printf(" ");
return 0;
}
Explanation / Answer
The program reads characters from user untill new line is entered i:e Enter is pressed and all the characters are stored in a character array line[100] of size 100.
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main(){
int i=0;
char line[100], c='a';
const char* null = "";
printf("Enter a line: ");
/*
Reads characters from user and store them in array line. Use i as index of array.
of size 100.
*/
while(c!=' '){
c = getchar();
line[i] = c;
i++;
}
// Since last character in the array is new line character so remove that character and fill all unfilled locations of array with null
for(i--;i<100;i++) strcpy(&line[i], null); // This loop moves from the last character location in array to end of array filling them with null
// Next iterate through the array and change every alphabet to upper case and put null at all special character's locations
for(i=0; i<100; i++){
if(isalpha(line[i])) line[i] = toupper(line[i]); // isalpha function Checks if character is alphabetic
else if(line[i]!= ' ') strcpy(&line[i], null); // if character is not alphabet or space then replace character with null
}
// Now print the modified character array
printf("Modified line: ");
for(i=0; i<100; i++){
printf("%c", line[i]);
}
printf(" ");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.