Solved

Quick C Problem

Posted on 2000-03-22
7
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

688 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