![]() |
| | #1 |
| Registered User Join Date: Jun 2009 Location: US of A
Posts: 300
| I have a doubt about string reversal. The code below reverses the string character by character but when i give it to print out the result there occurs an error. I am unable to figure out where am i going wrong. insert Code: #include <stdio.h>
#include <stdlib.h>
void xstrrev(char *);
int main(){
int i;
char *s[] = {
"To err is human...",
"But to really mess things up...",
"One needs to know C!!"
};
for(i=0;i<3;i++){
xstrrev(s[i]);
}
}
void xstrrev(char *a){
int len,j;
char *p;
printf("\n\r%s", a);
len = strlen(a);
printf("\n\r%d", len);
p = malloc(len+1);
for(j = 0;j<=len;j++){
p[j] = a[len-j];
printf("\n\r%c%c", a[len-j], p[j]);
if(j == len)
printf("\n\r%s", p);
}
}
|
| roaan is offline | |
| | #2 |
| Registered User Join Date: Apr 2006 Location: United States
Posts: 3,201
| When j == len in the last iteration of the loop, you will be accessing a out of bounds. This is because strlen counts from one when computing the length but array access starts from zero. Normally to iterate over an array you would write something like for ( i = 0; i < N; i++ ) ... Keep statement blocks like if ( j == len ) outside of the loop. 1. it will confuse you and 2. an if should not be necessary here, as the conditions under which your loop exists are obvious.
__________________ Os iusti meditabitur sapientiam Et lingua eius loquetur indicium "There is nothing either good or bad, but thinking makes it so." (Shakespeare, Hamlet, Act II scene ii) http://www.myspace.com/whiteflags99 Last edited by whiteflags; 07-03-2009 at 09:46 PM. Reason: wrong variable |
| whiteflags is offline | |
| | #3 |
| DESTINY Join Date: Jul 2008 Location: in front of my computer
Posts: 656
| I was also trying the same problem but with a different approach, but the result is a run time error. Here is my code: Code: #include <stdio.h>
#include <stdlib.h>
void xstrrev(char *a)
{
char t;
int x,i;
x=strlen(a);
for(i=0;i!=(x/2);i++)
{
x--;
t=a[i];
a[i]=a[x];
a[x]=t;
}
printf("%s\n",a);
}
int main()
{
int i;
char *s[] = {
"To err is human",
"But to really mess things up",
"One needs to know C!!"
};
for(i=0;i<3;i++)
xstrrev(s[i]);
}
And yes as the problem was similar so I didn't find it necessary to create a new thread for it. Hope its fine.
__________________ HOPE YOU UNDERSTAND....... for( ; ; ) printf("If you can't make it good, at least make it look good"); PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D. IDE- Microsoft Visual Studio 2008 Express Edition |
| BEN10 is offline | |
| | #4 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| One can't change a string literal. "One needs to know C!!" is constant; any attempt to change it will (usually/often) result in a runtime error. |
| tabstop is offline | |
| | #5 |
| DESTINY Join Date: Jul 2008 Location: in front of my computer
Posts: 656
| That means I can't do this problem by swapping the characters. Or is there a way to do it?
__________________ HOPE YOU UNDERSTAND....... for( ; ; ) printf("If you can't make it good, at least make it look good"); PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D. IDE- Microsoft Visual Studio 2008 Express Edition |
| BEN10 is offline | |
| | #6 |
| Registered User Join Date: Sep 2006
Posts: 2,501
| I wouldn't use that type of logic. An easy adaption might be: Code:
#include <stdio.h>
#include <stdlib.h>
void xstrrev(char *a)
{
char t;
int i, j;
j=strlen(a) - 1;
for(i=0; i < j; j--,i++)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
printf("%s\n",a);
}
int main()
{
int i;
char *s[] = {
"To err is human",
"But to really mess things up",
"One needs to know C!!"
};
for(i=0;i<3;i++)
xstrrev(s[i]);
printf("\n\n\t\t\t Press Enter When Ready ");
i = getchar();
return 0;
}
Last edited by Adak; 07-04-2009 at 01:50 AM. |
| Adak is offline | |
| | #7 |
| DESTINY Join Date: Jul 2008 Location: in front of my computer
Posts: 656
| But that's the main problem even the code you gave is not running.
__________________ HOPE YOU UNDERSTAND....... for( ; ; ) printf("If you can't make it good, at least make it look good"); PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D. IDE- Microsoft Visual Studio 2008 Express Edition |
| BEN10 is offline | |
| | #8 | |
| DESTINY Join Date: Jul 2008 Location: in front of my computer
Posts: 656
| Quote:
Code: #include <stdio.h>
int main()
{
int i;
char *s[] = {
"To err is human",
"But to really mess things up",
"One needs to know C!!"
};
char *a="BEN10";
s[2]=a;
printf("%s",s[2]);
}
__________________ HOPE YOU UNDERSTAND....... for( ; ; ) printf("If you can't make it good, at least make it look good"); PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D. IDE- Microsoft Visual Studio 2008 Express Edition | |
| BEN10 is offline | |
| | #9 |
| Registered User Join Date: Jun 2009 Location: US of A
Posts: 300
| insert Code: Quote:
#include <stdio.h>
int main()
{
int i;
char *s[] = {
"To err is human",
"But to really mess things up",
"One needs to know C!!"
};
char *a="BEN10";
s[2]=a;
printf("%s",s[2]);
}
A string literal cannot be changed is true. What you are trying to do above is that you are telling the compiler to give the address of string "BEN10" to char pointer a and then you are assigning that address to s[2] (array of pointers to strings). You are not at all touching or modifying the characters stored at that location. So you dont get any error !!!!! But i am still unable to do the string reversal. I have no idea whatsoever of where i am going wrong in the first code that i posted above ???????????????? |
| roaan is offline | |
| | #10 | |
| DESTINY Join Date: Jul 2008 Location: in front of my computer
Posts: 656
| Quote:
Code: #include<stdio.h>
#include<string.h>
int main(void)
{
char s[20]="my name is ben10",t;
//gets(s);
int i;
int len=strlen(s);
int x=len/2;
for(i=0;i!=x;i++)
{
len--;
t=s[i];
s[i]=s[len];
s[len]=t;
}
puts(s);
return 0;
}
__________________ HOPE YOU UNDERSTAND....... for( ; ; ) printf("If you can't make it good, at least make it look good"); PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D. IDE- Microsoft Visual Studio 2008 Express Edition | |
| BEN10 is offline | |
| | #11 | ||
| Registered User Join Date: Apr 2006 Location: United States
Posts: 3,201
| Quote:
Quote:
Earlier you had written char * s[], which declared s an array of char pointers, each element storing the address of a string literal. So when you passed s[i] to xstrrev, you were passing the literal. Now you've changed the code to char s[20], which is a typical string. but if you initialize s to something like "foobar" it's the same as this: Code: char s[20] = { 'f','o','o','b','a','r', '\0' }; /* not a string literal */
Hopefully you can extrapolate from this that you could use a matrix of char to get what you first posted, an array of strings, in a safe way. gets is also problematic.
__________________ Os iusti meditabitur sapientiam Et lingua eius loquetur indicium "There is nothing either good or bad, but thinking makes it so." (Shakespeare, Hamlet, Act II scene ii) http://www.myspace.com/whiteflags99 Last edited by whiteflags; 07-04-2009 at 07:39 AM. | ||
| whiteflags is offline | |
| | #12 |
| Registered User Join Date: Jun 2009 Location: US of A
Posts: 300
| insert Code:
#include <stdio.h>
#include <stdlib.h>
void xstrrev(char *);
int main(){
int i;
static char *s[] = {
"To err is human...",
"But to really mess things up...",
"One needs to know C!!"
};
for(i=0;i<3;i++){
xstrrev(s[i]);
printf("\n\r%s", s[i]);
}
}
void xstrrev(char *a){
int len,j;
char *p, temp;
//printf("\n\r%c", *a);
len = strlen(a);
//printf("\n\r%d %d", len, len/2);
p = a + len -1;
//printf("\n\r%d", p);
for(j = 1;j<= len/2;j++){
temp = *a;
printf("%c %c", temp , *p);
*a = *p;
*p = temp;
a++;
p--;
}
}
I tried usign this approach of pointers but still no success when i try to run it gives me an unhandled exception (i thought only java had exceptions) |
| roaan is offline | |
| | #13 |
| Registered User Join Date: Jul 2009
Posts: 40
| You need return 0 in your main function. edit: your header file as well #include <string.h> for you to use strlen() function |
| WatchTower is offline | |
| | #14 | |
| Registered User Join Date: Apr 2006 Location: United States
Posts: 3,201
| Quote:
If you wish to avoid using malloc, the simplest fix for your code is to use a matrix of char instead. Code: char s[3][512] = {
"To err is human...",
"But to really mess things up...",
"One needs to know C!!"
};
__________________ Os iusti meditabitur sapientiam Et lingua eius loquetur indicium "There is nothing either good or bad, but thinking makes it so." (Shakespeare, Hamlet, Act II scene ii) http://www.myspace.com/whiteflags99 | |
| whiteflags is offline | |
| | #15 |
| Registered User Join Date: Sep 2006
Posts: 2,501
| |
| Adak is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| String issues | The_professor | C++ Programming | 7 | 06-12-2007 09:11 AM |
| Custom String class gives problem with another prog. | I BLcK I | C++ Programming | 1 | 12-18-2006 03:40 AM |
| Classes inheretance problem... | NANO | C++ Programming | 12 | 12-09-2002 03:23 PM |
| creating class, and linking files | JCK | C++ Programming | 12 | 12-08-2002 02:45 PM |
| Warnings, warnings, warnings? | spentdome | C Programming | 25 | 05-27-2002 06:49 PM |