Solved

Quick C Problem

Posted on 2000-03-22
7
204 Views
Last Modified: 2010-04-15
Hi! The following program compiles correctly and even performs the first action (printing the string) just fine.  However, something is wrong with the function switchorder.  It is supposed to print out the previous string backwards, yet keeping the words spelled correctly.  For example the sentence: Hello everyone! should be printed everyone! Hello   Thanks a bunch!
#include <string.h>

#include <stdio.h>

void
scanline(char dest[], int dest_len)

        {
        int i, ch;

        i=0;

        for (ch=getchar();
             ch != '\n' && ch != EOF && i<dest_len-1;
             ch=getchar())
        dest[i++]=ch;
        dest[i]='\0';

        while (ch!='\n' && ch != EOF)
             ch=getchar();
        }

void
switchorder(char dest[], int dest_len)
        {
        int endwd, i, j;

        endwd=dest_len-1;

        for(i=dest_len-1; i<0;i--)
                {
                if(dest[i]==' ')
                        {
                        for(j=i; j>endwd; j++)
                                printf("%c", dest[j]);
                        printf(" ");
                        endwd=i-1;
                        }
                if(i==0)
                        for(j=0; j>endwd; j++)
        printf("%c\n\n", dest[j]);
                }

        }

int
main(void)

        {
        int dest_len=40;
        char dest[40];

        printf("Enter a string: ");

        scanline(dest, dest_len);

        printf("\n%s\n\n", dest);

        switchorder(dest, dest_len);

        }                          
0
Comment
Question by:SaraBiz
7 Comments
 
LVL 3

Expert Comment

by:marcjb
ID: 2646386
void switchorder(char dest[], int dest_len)
{
int endwd, i, j;
endwd=dest_len-1;

for(i=dest_len-1; i<0;i--)
   /*NEVER EXECUTES IF DESTLEN > 0 */
  I think you want i > 0
0
 
LVL 3

Expert Comment

by:marcjb
ID: 2646447
/*
You could also use a string reverse function to first reverse the string, and then reverse all the words back.  The following program does that.

hope this helps :)

*/

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

void strRev(char *s, int l)
{
char c;
char *start;
char *end;

for ( start = s, end = start + l - 1; start < end; start++, end-- )
    {
    c = *start;
    *start = *end;
    *end = c;
    }
}

void switchOrder(char *s)
{
char *p;
char *l;
strRev(s, strlen(s));
l = s;
while ( (p = strchr(l, ' ')) != NULL )
    {
    strRev(l, p-l);
    l = p + 1;
    }
strRev(l, strlen(l));
}


int main()
{
char a[]= "Hi how are you";
printf("%s\n", a);
switchOrder(a);
printf("%s\n", a);
return 0;
}
0
 

Author Comment

by:SaraBiz
ID: 2647473
Ok, I made that change (with the >) and a few others that were messing up those loops.  Now all the words print in backwards form, but there are some strange symbols right before it prints the first backwards word.  Why is this?
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 3

Expert Comment

by:marcjb
ID: 2649008
I don't think you should be passing dest_len to switchorder, as you are checking indexes in the character array that are garbage.  For example, if I only enter a string with 10 characters, you still check indexes 10-39.  I think you should be passing in the string length, not it's capacity.
0
 

Expert Comment

by:YuraPhink
ID: 2649444
exactly (-->marcjb),

use :
for(i=dest_len-1; i>0 && dest[i]!='\n' && dest[i]!=EOF;i--)



Yura.
               
0
 

Accepted Solution

by:
balakrishnan_t earned 50 total points
ID: 2652934
When u r sending the strlen as an argument to switch order, u have to send the exact length of the string typed and not the maximum length which is 40. so use a strlen function to find the string length and assign to dest_len.

Also u have messed up with some relational operators , so the entire loop is not executed since the condition is not satisfied. I have corrected those logical errors...try out the correct program

correct program

#include <stdio.h>

void
scanline(char dest[], int dest_len)

        {
        int i, ch;

        i=0;

        for (ch=getchar();
             ch != '\n' && ch != EOF && i<dest_len-1;
             ch=getchar())
        dest[i++]=ch;
        dest[i]='\0';

        while (ch!='\n' && ch != EOF)
             ch=getchar();
      }

void
switchorder(char dest[], int dest_len)
        {
        int endwd, i, j;

        endwd=dest_len-1;

        for(i=dest_len-1; i>=0;i--)
                {
                if(dest[i]==' ')
                        {
                  for(j=i; j<=endwd; j++)
                        printf("%c", dest[j]);
                  printf(" ");
                  endwd=i-1;
                  }
            if(i==0)
                for(j=0; j<=endwd; j++)
                printf("%c", dest[j]);
            }

        }

int
main(void)

        {
        int dest_len=40;
        char dest[40];

        printf("Enter a string: ");

        scanline(dest, dest_len);

        printf("\n%s\n\n", dest);

      dest_len=strlen(dest);
        switchorder(dest, dest_len);

        }                            

0
 
LVL 84

Expert Comment

by:ozo
ID: 2654259
null
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question