Re: Code does not work!!!
You are trying to modify a string literal ("VENICE"). This will cause undefined behaviour.
Re: Code does not work!!!
So....how can I change my code so that it works??? :confused:
Re: Code does not work!!!
Either:
1) Change to use std::string instead of char arrays, or
2) Create a char array on the heap using new and initialize this array to contain "VENICE" (plus the null-terminator)
Re: Code does not work!!!
You mean like that:
Code:
#include <stdio.h>
#include <string.h>
void func(char *p1,char *p2){
char ch;
while (p1<p2){
ch=*p1;
*p1=*p2;
*p2=ch;
p1++;
p1--;
}
}
int main()
{
char a[8];
printf("Give a string:\n");
gets(a);
puts(a);
func(a,a+4);
puts(a);
return 0;
}
????The output is still wrong :(
Re: Code does not work!!!
You have a little bug in func(). Run it through the debugger -you'll spot it eventually... :D
Also, don't use gets() - it's dangerous, and has been deprecated. For input it is safer to use streams (i.e. cin or getline)
Re: Code does not work!!!
I haven't got it and also I haven't got taught streams.Could you explain me again??? :blush:
Re: Code does not work!!!
Quote:
Originally Posted by
mathmari
I haven't got it and also I haven't got taught streams.Could you explain me again??? :blush:
Streams are used in c++ - not in c which I understand from your previous posts you are using. Instead of using gets, it's better to use fgets for which you specify the maxmimum number of chars to get and also null terminates the got string.
Replace
with
Code:
fgets(a, 8, stdin);
In your func()
Are you sure you want to increment p1 and then decrement it?????:ehh:
Re: Code does not work!!!
Ok...nice...Oh no,I am sorry I wanted to decrement p2!!!!
Re: Code does not work!!!
I changed the code:
Code:
#include <stdio.h>
#include <string.h>
void func(char *p1,char *p2){
char ch;
while (p1<p2){
ch=*p1;
*p1=*p2;
*p2=ch;
p1++;
p2--;
}
}
int main()
{
char a[8];
printf("Give a string:\n");
gets(a);
puts(a);
func(a,a+4);
puts(a);
return 0;
}
and now the output is right....Thank you very much!!!!!!!!!! :p
Re: Code does not work!!!
But your code still has some problems. You are still using gets() rather than fgets(). Also with func(a, a + 4), what if the user enters a string with less than 5 characters?
Re: Code does not work!!!
Quote:
Originally Posted by 2kaud
Streams are used in c++ - not in c
Technically, C has the notion of input/output streams too, but they would not be the C++ I/O streams that Peter_B mentioned, but rather say, stdin and in this case the use of fgets.
Re: Code does not work!!!
I want that my code works,for the string VENICE...If I use fgets,do I not have to create a file???
Quote:
Originally Posted by
2kaud
But your code still has some problems. You are still using gets() rather than fgets(). Also with func(a, a + 4), what if the user enters a string with less than 5 characters?
Re: Code does not work!!!
Quote:
Originally Posted by mathmari
If I use fgets,do I not have to create a file?
No, you don't. stdin is already acts as a file pointer.
Re: Code does not work!!!
Ah ok...I haven't get taught stdin yet :)