Solved

Quick C Problem

Posted on 2000-03-22
7
203 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
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

Are end users causing IT problems again?

You’ve taken the time to design and update all your end user’s email signatures, only to find out they’re messing up the HTML, changing the font and ruining the imagery. What can you do to prevent this? Find out how you can save your signatures from end users today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
In desperate need of help 8 132
why "." vs "->" 23 119
xamarin c# deserialize Json containing nested object 2 80
Using ANSI C how to Read a .csv file 10 69
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 tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

929 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now