[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Quick C Problem

Posted on 2000-03-22
7
Medium Priority
?
210 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
The eGuide to Automating Firewall Change Control

Today’s IT environment is constantly changing, which affects security policies and firewall rules. Discover tips to help you embrace this change through process improvement & identify areas where automation & actionable intelligence can enhance both security and business agility.

 
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 200 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 85

Expert Comment

by:ozo
ID: 2654259
null
0

Featured Post

Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses

591 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