copy string into another string - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

copy string into another string

Anago_PeeAnago_Pee Posts: 1Member
Hey guys am a beginner in C++ and has this as an assignment i really need your help to make my marks.

1. Write a program that will reverse an accepted string and copy it into another string.

2. write a program to accept and display the amount outstanding for 10 customers. the amount outstanding should be displayed in an ascending order.

Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]
    The string is an array, so it can be indexed as an array. And indexes begin with zero, not with one as in FORTRAN:

    Example:
    [/color]
    [code]
    char s [] = "HE'S DEAD, JIM.";

    char c1 = s[0]; [color=Green]// c1 now will contain a character 'H'.[/color]
    char c2 = s[3]; [color=Green]// c2 now will contain a character 'S'.[/color]
    [/code]
    [color=Blue]Now, using that fact, you can reverse a string AND copy it in one loop:[/color]
    [code]
    char destination [32];
    char source [32]; [color=Green]// input that string from user (scanf,gets, etc.)[/color]
    int iSrc, iDest;
    [color=Green]//
    // Find the end of the source string
    //[/color]
    iSrc = 0;
    while (source [iSrc] != 0) iSrc++;
    [color=Green]//
    // Now copy characters in reverse order
    //[/color]
    iDest = 0;
    while (iSrc > 0)
    {
    destination [iDest++] = source [[color=Purple]--iSrc[/color]];
    }
    [color=Green]//
    // Put terminating zero into destination string
    //[/color]
    destination [iDest] = 0;
    [/code]
    [color=Blue]Now, since I did your homework - you have a new one: find out why [color=Purple]purple[/color] text says "--iSrc" and not "iSrc--"?

    For sorting items - use the bubble sort:
    [link=http://en.wikipedia.org/wiki/Bubble_sort#Pseudocode_implementation]http://en.wikipedia.org/wiki/Bubble_sort#Pseudocode_implementation[/link]
    [/color]
Sign In or Register to comment.