I'm trying to write a program to reverse a string, letter by letter. This was my latest attempt:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
    char oldMsg[100], newMsg[100];
    int cd = 12, cu = 0;
    strcpy(newMsg, "Hello World!");
    while (cd > 0)
    {
          newMsg[cu] = oldMsg[cd];
          cu++;
          cd--;
    }
    printf("%c", newMsg);
    printf("\n\nPress ENTER to end . . .");
    getchar();
    return 0;
}

The final message is a 'p', which I'm not sure where it came from. Can I get some help please?

Problem is you're copying old to new, whereas what you want to do is new to old. Also using wrong formatting in printf().

See the comments inside the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//to avoid hard coding.
#define MY_MSG_LEN 100

int main(void)
{
    char oldMsg[MY_MSG_LEN], newMsg[MY_MSG_LEN];
    int len_newMsg = 0, i ;
    //not required to hardcode the length of "Hello World!", use strlen() instead
    //int cd = 12, cu = 0;
    strcpy(newMsg, "Hello World!");

    // -1 to discount the null char, which we'll still like to have at the end
    //and not at the begenning of oldMsg.
    len_newMsg = strlen( newMsg ) - 1 ;

//copy the other way around.
/*    while (cd > 0)
    {
          newMsg[cu] = oldMsg[cd];
          cu++;
          cd--;
    }
*/
    for( i = 0; i <= len_newMsg; i++ )
       oldMsg[i] = newMsg[len_newMsg-i] ; //copy backwards

    //last char should be a null char
    oldMsg[len_newMsg+1] = '\0' ;

    //%c is for a single character, %s is for a c-style char string
    //printf("%c", newMsg);
    printf("%s", oldMsg);

    printf("\n\nPress ENTER to end . . .");
    getchar();

    return 0;
}

hi this is the program without using any string builtin function......

/* C program without using string functions*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char str1[20],str2[20],c;
int i,j,count=0,k;
printf("Enter the String");
i=0;
while((c=getchar())!='\n')
{
str1[i]=c;
i++;
count++;
}
str1[i]='\0';
j=0;
for(i=count-1;i>=0;i--)
{
str2[j]=str1[i];
j++;
}
str2[j]='\0';
printf("string1 = %s\nstring2 = %s",str1,str2);
getch();
}

ranjani, I'm going to ask you nicely just once to use code tags when you post code. Code tags put your code in a nice block and retain any formatting you had. Not using them makes your code an awful mess that nobody wants to read.

:)

ranjani, I'm going to ask you nicely just once to use code tags when you post code. Code tags put your code in a nice block and retain any formatting you had. Not using them makes your code an awful mess that nobody wants to read.

you mean like this attractives?

/* C program without using string functions*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char str1[20],str2[20],c;
int i,j,count=0,k;
printf("Enter the String");
i=0;
while((c=getchar())!='\n')
{
str1[i]=c;
i++;
count++;
}
str1[i]='\0';
j=0;
for(i=count-1;i>=0;i--)
{
str2[j]=str1[i];
j++;
}
str2[j]='\0';
printf("string1 = %s\nstring2 = %s",str1,str2);
getch();
}

or can you give some model? i am a new one for this forum

>or can you give some model?
The text editor has a watermark that tells you exactly how to use code tags. I don't know how you could miss that, but here's a link to instructions you can't miss.

Ok, I see what I was doing wrong. Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.