Solved

Quick C Problem

Posted on 2000-03-22
7
202 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
Comment Utility
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
Comment Utility
/*
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Expert Comment

by:marcjb
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
null
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

744 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

17 Experts available now in Live!

Get 1:1 Help Now