473,383 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How to avoid garbage values at the time of output

I am trying to code a C program which will print the reverse of an entered string.
My code is as follows:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. void reverse(char[]);
  5. int mystrlen(char[]);
  6.  
  7. int mystrlen(char s[])
  8. {
  9.     int i;
  10.     for(i=0;s[i]!='\0';i++);
  11.     return i;
  12. }
  13.  
  14. void reverse(char s[])
  15. {
  16.      int i;
  17.      char rev[100];
  18.      for(i=mystrlen(s);i>1;i--)
  19.      rev[mystrlen(s)-i-1]=s[i];
  20.      rev[mystrlen(s)]='\0';
  21.      printf("%s", rev);
  22.      }
  23.  
  24. int main()
  25. {
  26.     char str[100];
  27.     printf("Enter a string:");
  28.     fflush(stdin);
  29.     gets(str);
  30.  
  31.     printf("The reverse of %s is:", str);
  32.     reverse(str);
  33.  
  34.     getch();
  35.     return 0;
  36. }
However when I am entering Hello World, the output I am getting is dlroW ollA.

Please provide me a solution to overcome this.
Mar 20 '11 #1
2 10147
donbock
2,426 Expert 2GB
Consider input string "Hello World\0". mystrlen ought to return 11 for this string.

Look at the loop in reverse:
Expand|Select|Wrap|Line Numbers
  1. for(i=mystrlen(s);i>1;i--)
  2.    rev[mystrlen(s)-i-1]=s[i];
  3. rev[mystrlen(s)]='\0';
Unroll the loop and we see that you have:
Expand|Select|Wrap|Line Numbers
  1. rev[-1] = s[11];   // '\0'
  2. rev[0] = s[10];    // 'd'
  3. rev[1] = s[9];     // 'l'
  4. rev[2] = s[8];     // 'r'
  5. rev[3] = s[7];     // 'o'
  6. rev[4] = s[6];     // 'W'
  7. rev[5] = s[5];     // ' '
  8. rev[6] = s[4];     // 'o'
  9. rev[7] = s[3];     // 'l'
  10. rev[8] = s[2];     // 'l'
  11. // Exit the loop.
  12. rev[11] = '\0';
Notice that rev[9] and rev[10] are uninitialized. Writing to rev[-1] is a bad thing.
Mar 21 '11 #2
i have modified the code to:

Expand|Select|Wrap|Line Numbers
  1. void reverse(char s[])
  2. {
  3.      int i;
  4.      char rev[100];
  5.      for(i=0;i<mystrlen(s);i++)
  6.      rev[i]=s[mystrlen(s)-1-i];
  7.      rev[i]='\0';
  8.      printf("%s", rev);
  9.      }
This has solved the problem. :)
Mar 21 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: tomektomeknyc | last post by:
I did an interactive program to input and store employee paycheck information including total net pay into txt file. I can read it from txt file but how can i lets say modify it to calculate the...
0
by: indacrypt | last post by:
Hi, I'll post detailed code if required, but if someone has alreayd had this kind of a problem before and can help me out, that'll bemost appreciated. Theres a particular function in my program...
1
by: Scott | last post by:
I have a query that calculates the Total Time spent working on a project. As an employee can have multiple time entries for a project, the "total Times" are summed in a report using the following...
10
by: KevinL | last post by:
I'm writing a random number generator program and using time() to seed it. I found out that time() output is in seconds. Do you know of another function that outputs in thousands of seconds ? ...
3
by: pratap | last post by:
I have an application which periodically gives me an output of sonar values .However i am unable to view the values as they get replaced by the new ones below in realtime and are displayed after...
1
by: Gilberto | last post by:
Hello, in several forms i have a NEXT, PREVIOUS and SAVE command buttons which were working perfectly...no errors showing...nothing!!! i had to convert my DB to 2003 so i could create the .mde. I did...
87
by: pereges | last post by:
I have a C program which I created on Windows machine. I have compiled and executed the program on windows machine and it gives me the consistent output every time i run it. for eg. input a = 2,...
9
by: Aslam Shah | last post by:
I have a form with 150 checkboxes. Need to send email with the Checked ones usng ASP. But i am unable to figure a simple way to do it. All i can understand is getting their values one by one and...
2
by: eneyardi | last post by:
I have a form that shows the current time, My problem is when users change the system time and it will update the time in the program. Is there any idea on how to avoid users change the system time?...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.