Need Help in 'C'

badtnc

Member
Jul 17, 2007
3,980
51
0
Nugegoda, Anuradhapura
i want to revers a arry and store it in another arry and want to print it.

this is the code

some problem in it.
can some one modify it???

is dis rev_str[n-i]=str; correct???

#include <stdio.h>
# include <string.h>

main()

{

int i,m,n,count;
char str[10],rev_str[10];

printf("Enter a String :");
gets(str);

for(i=0;str!='\0';i++)
{

}

m=i;
n=i;
for(i;i>0;i--)
{

rev_str[n-i]=str;

}


printf("new: %s",rev_str);

}
 

x-pert

Member
Jun 13, 2006
20,952
77
0
You need to find the array size before doing anything.
So using strlen be a good way than using =! '\0'

And then:

hint only:

Code:
for(i=0;str[i]!='\0';i++) [COLOR=DarkGreen] // your code[/COLOR]
j=i


for(p=j-1, i=0; p>=0; p--, i++)
 rev_str[i]=str[p];
 
Last edited:

badtnc

Member
Jul 17, 2007
3,980
51
0
Nugegoda, Anuradhapura
x-pert said:
You need to find the array size before doing anything.
So using strlen be a good way than using =! '\0'

And then:

hint only:

Code:
for(i=0;str[i]!='\0';i++) [COLOR=DarkGreen] // your code[/COLOR]
j=i


for(p=j-1, i=0; p>=0; p--, i++)
 rev_str[i]=str[p];


#include <stdio.h>
# include <string.h>

main()

{

int i,m,n,count;
char str[10],rev_str[10];

printf("Enter a String :");
gets(str);

for(i=0;str!='\0';i++) // this is for find the arry size. i will store the arry size
{

}

m=i;
n=i;
for(i;i>0;i--) //this is for reverse. but not working
{

rev_str[n-i]=str;

}


printf("new: %s",rev_str);

}
 

x-pert

Member
Jun 13, 2006
20,952
77
0
PM ekata answer eka methana dennam machang ooni kenekta balaaganna and mama waradi nam danna kenekta hadanna

Problem of the below:

Code:
#include <stdio.h>
# include <string.h>

main()

{

int i,m,n,count;
char str[10],rev_str[10];

printf("Enter a String :");
gets(str);

for(i=0;str[i]!='\0';i++)
{

}

m=i;
n=i;
for(i;i>0;i--)
	{

		rev_str[n-i]=str[i];  

[B][COLOR=DarkGreen]// Logic is a little bit wrong machang as I can see. 
// Let's go in to the loop
// step 1: for i; i>0; i-- 
// Let's say the array size is 6. So condition, i > 0 satisfies.
// Then i-- = 5
// Then, rev_str[6-5] = str[i] Which means, rev_str[1] = str[5]

//Second loop
// now i = 5. 
// So rev_str[6-4] = str[4] Which means, rev_str[2] = str[4] 

// and so on...

// So what happens to the first item of the array? (rev_str[0]?)[/COLOR][/B] 


	}


printf("new: %s",rev_str);

}
 

badtnc

Member
Jul 17, 2007
3,980
51
0
Nugegoda, Anuradhapura
wat about this

#include <stdio.h>
# include <string.h>

main()

{

int i,m,n,count;
char str[10],rev_str[10];

printf("Enter a String :");
gets(str);

for(i=0;str!='\0';i++)
{

}

m=i;
n=i;
int y=0
for(i;i>0;i--)
{

rev_str[y]=str;
y++;

}


printf("new: %s",rev_str);

}
 

badtnc

Member
Jul 17, 2007
3,980
51
0
Nugegoda, Anuradhapura
machan check dis.
dis should work rit... bt it wont :-(

#include <stdio.h>
# include <string.h>

main()

{

int i,m,n,count,y=0;
char str[10],rev_str[10];

printf("Enter a String :");
gets(str);

for(i=0;str!='\0';i++)
{

}

m=i;
n=i;

for(i;i>=0;i--)
{
rev_str[y]=str;
y++;

}


printf("new: %s",rev_str);

}
 

x-pert

Member
Jun 13, 2006
20,952
77
0
badtnc said:
wat about this

#include <stdio.h>
# include <string.h>

main()

{

int i,m,n,count;
char str[10],rev_str[10];

printf("Enter a String :");
gets(str);

for(i=0;str!='\0';i++)
{

}

m=i;
n=i;
int y=0
for(i;i>0;i--)
{

rev_str[y]=str;
y++;

}


printf("new: %s",rev_str);

}


Come-on machang... Don't 'just' ask.....

Check the Logic

Code:
#include <stdio.h>
# include <string.h>

main()

{

int i,m,n,count;
char str[10],rev_str[10];  
[B][COLOR=DarkGreen]// Oh didn't see this earlier. So you're creating an array of size 10. Then again use the below 1st for loop to check the size...... 
// I'm not sure whether it's possible in C. 
// Just ask to enter an array. And then calculate the size by using strlen function. [/COLOR][/B]

printf("Enter a String :");
gets(str);

for(i=0;str[i]!='\0';i++)
{

}

m=i;
n=i;
int y=0
for(i;i>0;i--)
    {

        rev_str[y]=str[i];
        y++;

[B][COLOR=DarkGreen]//So in here.... 
// 1st loop:
// i=3 (let's say)
// m = n = 3 right?
// y = 0

// for i=3; i>0; i--;
// Condition is true. Hence,
// rev[0] = str[3]
// y = 1 now. 

// 2nd loop:
// i = 2 now. 
// Hence m and n are out of the for loop, m and n don't get changed. So why are you using those 2 variables? 
// Anyway, condition is true. Hence,
// rev[1] = str [2] 

// Next loop
// rev[2] = str [1]

// In the next loop, condition becomes 0>0. False. 
// So we can't get the last value of the array. [/COLOR][/B]



    }


printf("new: %s",rev_str);

}
 

badtnc

Member
Jul 17, 2007
3,980
51
0
Nugegoda, Anuradhapura
machan dis is da code
dis works
bt small problem

#include <stdio.h>
# include <string.h>

main()

{

int i,m,n,count;
char str[10],rev_str[10];

printf("Enter a String :");
gets(str);

for(i=0;str!='\0';i++)
{

}
m=i;
int y=-1; //if i put y=0 it wont work. why is dat???????????????
for(i;i>=0;i--)
{
rev_str[y]=str;
y++;


}

for(i=0;i<m;i++)
printf("%c",rev_str);

}
 

badtnc

Member
Jul 17, 2007
3,980
51
0
Nugegoda, Anuradhapura
this is the code...
#include <stdio.h>
# include <string.h>

main()

{

int i,m,n,count;
char str[10],rev_str[10];

printf("Enter a String :");
gets(str);

for(i=0;str!='\0';i++)
{

}
m=i;
int y=0;
i=i-1;
for(i;i>=0;i--)
{
rev_str[y]=str;
y++;


}

for(i=0;i<m;i++)
printf("%c",rev_str);

}

tx everyone