Well, I'll be blunt with you guys. This is an assignment. It was given a week ago. I did everything according to the instructions given, then suddenly my professor said the whole thing is wrong. I mean, it's giving the right output, but he says I didn't do it the way he wanted me to. He said we should use parameters. Thing is, I don't know how. Our previous professor didn't teach us about parameters, so i have no idea how to do it. I hope someone might help. Or please change at least 1 function into a parameter type. I have a defense about my program 2 days from now. Btw, the reason it's so long is because we weren't allowed to use built-in functions.
Really? Well, my prof turned it into this. And the "hello" is just an example. He wants it to be user input. I don't know how to make the rest of the functions like he did on LENGTH. The first code on my post is what I did. This second one is when he changed it into something I don't know how to do to the rest or make it into user input.
#include<stdio.h>
#include<conio.h>
#include<windows.h>
#define p printf
#define s scanf
struct STRING{
char mystring[100];
char CH;
char CH2;
char text[225];
int index;
int i;
int length;
int choice;
};
typedefstruct STRING str;
typedef str *string;
int LENGTH(char input[]);
void CHARAT(str &st);
void REPLACE(str &st);
main()
{
str st; //structure variable
string ptr; //pointer variable
ptr=(string)malloc(sizeof(str));
//main menu
do
{
system("cls");
p("*Choose your Pokemon!*");
p("\n\nFUNCTIONS:");
p("\n\n\t\t [1] LENGTH()");
p("\n\t\t [2] CHARAT()");
p("\n\t\t [3] REPLACE()");
p("\n\n\t\t SELECT A FUNCTION: ");
fflush(stdin);
s("%i",&st.choice);
switch(st.choice)
{
case 1:
case 1:
printf("String length is %i",LENGTH("hello")); break;
case 2:
CHARAT(st);break;
case 3:
REPLACE(st);break;
}
p("\n\nPress Y if you want to choose another function.\nPress N if you want to exit. [Y/N]: ");
fflush(stdin);
s("%c",&st.CH);
}
while(st.CH=='y'||st.CH=='Y');
}
void LENGTH(str &st)
{
system("cls");
p("\n Enter String: ");
fflush(stdin);
gets(st.mystring);
st.length=0;
st.index=0;
while(st.mystring[st.index]!='\0')
{
st.length++;
st.index++;
}
p("\n String length is: %i",st.length);
}
void CHARAT(str &st)
{
system("cls");
p("\n Enter String: ");
fflush(stdin);
gets(st.mystring);
p("\n Index of the character to grab: ");
fflush(stdin);
s("%i",&st.index);
p("\n The character is: %c",st.mystring[st.index-1]);
}
void REPLACE(str &st)
{
system("cls");
p("\n Enter String: ");
fflush(stdin);
gets(st.mystring);
st.index=0;
p("\n Character you want to change: ");
fflush(stdin);
s("%c",&st.CH);
p("\n Character you want to replace: ");
fflush(stdin);
s("%c",&st.CH2);
p("\n\nOutput: ");
while(st.mystring[st.index]!='\0')
{
if(st.mystring[st.index]==st.CH)
{
p("%c",st.CH2);
}
else
{
p("%c",st.mystring[st.index]);
}
st.index++;
}
}
> He said we should use parameters. Thing is, I don't know how.
The issue is that the parameters you are using are bogus; they are not used for either passing information to the function or retrieving information from it.
For instance, your LENGTH function could very well have been written as:
1 2 3 4 5 6 7 8 9 10
//void LENGTH(str &st)
void LENGTH()
{
str st ;
printf("\n Enter String: ");
gets(st.mystring);
// ...
}
Instead, get the string from the user in main() and pass the initialized string to the function.
And so on, for the other functions.
I see. I kinda got it, but he said that he didn't want us to use void functions. He wanted us to use parameters. And I really have no idea how to do it.
// input: initialized STRING st; initialize it in main()
// result: length of string
int LENGTH( str &st )
{
st.length=0;
st.index=0;
while(st.mystring[st.index]!='\0')
{
st.length++;
st.index++;
}
return st.length ; // print out the returned value in main()
}