|
-
Oct 16th, 2002, 02:34 AM
#1
Thread Starter
New Member
how to return a string in function?
I have try this for 10 time but it can't work and I'am sure my logic is correct, is this coding correct::
#include<stdio.h>
#include<conio.h>
//======================================
char* Decodestr(char* encodedstr,char* codestr);
//======================================
void main(){
char encodedstr[6]=" ";
char codestr[6]=" ";
clrscr();
printf("Enter String:");
scanf(" %s",&encodedstr);
printf("Enter Code:");
scanf(" %s",&codestr);
Decodestr(encodedstr,codestr);
printf("Encoded Result Is:%s");
getch();
}
//========================================
char* Decodestr(char* encodedstr,char* codestr){
char *p1, *p2;
p1=encodedstr;
p2=codestr;
char ans[6]=" "
while(*p1!='\0'){
ans[c]=*p1 + *p2;
p1++;
p2++;
c++;
}
return;
}
//======================================
-
Oct 16th, 2002, 03:44 AM
#2
Addicted Member
Depends what you want to do.
First: you may wish to ignore me as I am known for being foolish
Second: There are some syntax errors etc:
clrscr() - does this work still - doesn't on mine. (may not be a problem)
No ';' on line - char ans[6]=" "
'c' not declared for - ans[c]=*p1 + *p2; need int c = 0;
printf("Encoded Result Is:%s"); - %s doesn't refer to anything.
return; doesn't return anything. What do you want to return - the ans[] variable. You don't assign the function to anything anyway.
Hope this helps
HD
-
Oct 16th, 2002, 08:42 AM
#3
You can't return strings this way. Search the forum for this, it has been discussed quite a few times.
Remove the & in the calls to scanf. Or better yet, use gets (or even better fgets(stdin, buffer) ) for reading strings. fgets provides bounds checking and is faster than formatted input.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 16th, 2002, 09:29 PM
#4
Lively Member
I'll correct it maybe it will work not so sure coz didn't touch C for sometimes:
#include<stdio.h>
#include<conio.h>
//======================================
char* Decodestr(char* encodedstr,char* codestr);
//======================================
void main(){
char encodedstr[6]=" ";
char codestr[6]=" ";
clrscr();
printf("Enter String:");
scanf(" %s",&encodedstr);
printf("Enter Code:");
scanf(" %s",&codestr);
printf("Encoded Result Is:%s",Decodestr(encodedstr,codestr));
getch();
}
//========================================
char* Decodestr(char* encodedstr,char* codestr){
char *p1, *p2;
p1=encodedstr;
p2=codestr;
char ans[6]=" ";
int c=0;
while(*p1!='\0'){
ans[c]=*p1 + *p2;
p1++;
p2++;
c++;
}
return ans;
}
//======================================
-
Oct 17th, 2002, 05:15 AM
#5
It will not reliably. Again, you can't return strings this way.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 17th, 2002, 08:11 AM
#6
Lively Member
hmmmmmm... issit?
not quite remember but I've done that before. Only have partial memory about it that to return a string the start of the function will have:
char* Decodestr(char* encodedstr,char* codestr){
......}
as the return statement I forgot how whether an * needed or not.
Or maybe can u provide the code? if possible.
-
Oct 17th, 2002, 08:49 AM
#7
Code:
char * strToUpper(const char *instr, char *outstr)
{
const char *p = instr;
char *q = outstr;
while(*p) {
*q++ = toupper(*p++);
}
*q = 0;
return outstr;
}
// To call:
char buffer[30];
strToUpper("weird", buffer);
The return value is the same as outstr, it's only provided for convenience, so that you can do things like
char * str = strToUpper("bad", malloc(10));
or pass the return value to another function like
char buf1[100], buf2[30];
strcpy(buf1, "This is ");
strcat(buf1, strToUpper("good", buf2));
// buf1 is now "This is GOOD"
You should not do the malloc thing as it doesn't give you a chance to check the malloc return.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 17th, 2002, 09:24 PM
#8
Lively Member
ohhhhh... now it kinda make sense to me
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|