[SOLVED] Thanks to Grey WolfTrying to get a char to reverse

Alright quick question I can get this to work but after the "char s[] = "The quick brown fox ";" I need a space after fox whats wrong in my code thats making it that I need a space. Im stumped how to make it terminate with NULL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>

void main()
{
	int begin = 0;
	int end = 0;
	int space = 0;
	char s[] = "The quick brown fox ";
	
	int i = 0;

	do
	{
		if(s[i] != 32)
		{
			i++;
		}
		else
		{
			char temp;						
			begin = space;					
			end = i;						
			space = end + 1;				
			while(end >= begin)			
			{								
				temp = s[begin];			
				s[begin] = s[end];			
				s[end] = temp;				
				begin++;					
				end--;						
			}
		}
	}while(s[i] != NULL);

	for(int index = 0; index < i; ++index)
	{
		printf("%c", s[index]);
	}
	printf("\n\n");
}
Last edited on
You probably need to add a \0 at the end. (I'm pretty sure they aren't added by default)
Nope that doesn't help :( I mean with the space the output is fine lol. But it doesn't make the problem itself correct.
Try with if ((s[i] != 32) || (s[i] != '\n'))
1. nul and NULL are two entirely different things.
2. All string literals end with '\0'.

Mythios: You're comparing a char to a NULL pointer. Change the NULL in your comparison to 0 or '\0' (they both mean the same, so it doesn't make a difference). 0 is the nul character.
OK so say I do it how your saying (my bad with the nul there, should of picked that one up myself) I'm still getting no errors but the output of the program will be this:
The quick brown fox

ehT kciuq nworbfox


So it's stumped me why it's not printing it out how I need. Yet if I make the input have a space at the end it works fine.

The current code I'm using is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>

void main()
{
	int begin = 0;
	int end = 0;
	int space = 0;
	char s[] = "The quick brown fox";

	for(int i = 0; i < 19; ++i)
	{
		printf("%c", s[i]);
	}

	int i = 0;

	do
	{
		if(s[i] != 32 && s)
		{
			i++;
		}
		else
		{
			char temp;						
			begin = space;					
			end = i;						
			space = end + 1;				
			while(end >= begin)			
			{								
				temp = s[begin];			
				s[begin] = s[end];			
				s[end] = temp;				
				begin++;					
				end--;						
			}
		}
	}while(s[i] != '\0' && s);

	printf("\n\n");

	for(int index = 1; index < i; ++index) //Original 0
	{
		printf("%c", s[index]);
	}
	printf("\n\n");
}
s itself will never be false, so those conditions on 19 and 38 are useless.

You just need to reverse the letters in each word?

Why not just write two functions - the first one, given a string and a starting index, finds the index of the first space occuring after the start
index, and the second, given a string and two indices reverses all of the characters in the range?

That would be far simpler than nested loops.
Same as the other program I'm doing. They are basic but they have to be done a cert ant way. Not exactly the way I'd do them but I have to follow the guideline.
The guideline is that you can't use functions?
Its the very beginning of this course we technically haven't learned them yet lol. So it's not that we can't we are meant to use what we know. I have a lot more knowledge otherwise. It's just a little harder to attack these problems in a way like this if you get what I mean :)
F that. Use all the knowledge you have. Not being supposed to know something doesn't mean not being supposed to use that something.
Last edited on
Ah well, I agree but nothing I can do about it until a couple of weeks lol. His just running through the basics as some people don't know a lot about it. So I have to sit back and enjoy the ride of using no advancedness lol.

Cheers guys.
Really though. Do as helios suggested. Or at least ask your professor.

In my intermediate programming class we used Turbo Pascal. Most of the class was teaching things I already knew and had been using for years. In fact, I had developed a text-based UI similiar to Turbo Vision before even starting the class, so to make the homework assignments the least bit challenging I wrote all of my homework assignments to use my UI instead of just writeln.

My UI library was OO with some assembler. Turbo Pascal's OO extensions were taught the last few weeks of the semester; assembler wasn't touched upon until the following semester (systems programming).

If your skills are well beyond the class, why don't you see if you can take the final exam early and place out of the class if it is a prerequisite?
closed account (z05DSL3A)
So I have to sit back and enjoy the ride of using no advancedness lol.

So with you basic skills you should be able to do it with your eyes closed. ;0)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>

void main()
{
    int begin = 0;
    int end = 0;
    int space = 0;
    char s[] = "The quick brown fox";

    for(int j = 0; j < 19; ++j)
    {
        printf("%c", s[j]);
    }

    int i = 0;

    bool stop = false;
    while(!stop)
    {
        bool foundSpace = false;

        if(s[i] == 32)
            foundSpace = true; 

        if(s[i] == '\0')
        {
            foundSpace = true;
            stop = true;
        }

        if(foundSpace) 
        {
            char temp;
            space = i;				
            end = space -1;										
            while(end >= begin)			
            {								
                temp = s[begin];			
                s[begin] = s[end];			
                s[end] = temp;				
                begin++;					
                end--;						
            }
            begin = space+1;    
        }
        i++;

    }//End while

    printf("\n\n");

    for(int index = 0; index < i; ++index) //Original 0
    {
        printf("%c", s[index]);
    }
    printf("\n\n");

}
Last edited on
Haha, well one would think so but I managed to get stumped lol. Thanks for that - i feel like a "retard" now thank you :)

hey for greet output you just need

1
2
3
4


strlen=index;


im sure you will be amze..
Topic archived. No new replies allowed.