Results 1 to 7 of 7

Thread: Basic C Question :(

  1. #1
    Odc
    Odc is offline
    Sonic Boom! Odc's Avatar
    Join Date
    Aug 2005
    Location
    Hertfordshire
    Posts
    1,537
    Thanks
    26
    Thanked
    18 times in 17 posts
    • Odc's system
      • Motherboard:
      • Gigabyte Z77-D3H
      • CPU:
      • Core i5 3570k
      • Memory:
      • 8GB Corsair XMS3
      • Storage:
      • 500GB Corsair MX100, 500Gb Samsung
      • Graphics card(s):
      • SLI Zotac GTX 560 Ti 448
      • PSU:
      • 750W Enermax
      • Case:
      • CoolerMaster Centurion 5
      • Operating System:
      • Windows 7 Professional x64
      • Monitor(s):
      • Dell SP2309W
      • Internet:
      • BT Infinity 2

    Basic C Question :(

    Hi guys,

    I'm trying to create a simple function that reverses a string without using any in-built functions. I have code that should work but it just crashes on the first loop. I have no idea whats wrong and its rather frustrating being such an easy problem! Any ideas?


    #include <iostream>

    void reverse(char *p)
    {
    char temp, *pCopy;
    int i, j, sizeOfString;

    sizeOfString = 0;
    pCopy = p;

    while(*pCopy != '\o') // Calculates size of p or pCopy
    {
    sizeOfString ++;
    *pCopy ++;
    }

    for(i = 0, j = sizeOfString; i < j; i ++, j --) // Reverses the string
    {
    temp = p[i];
    p[i] = p[j];
    p[j] = temp;
    }
    }

    int main()
    {
    char *testString = "Hello";

    reverse(testString);

    std::cout << testString << std::endl;

    return 0;
    }


    Hexus Trust = Odesi

  2. #2
    Odc
    Odc is offline
    Sonic Boom! Odc's Avatar
    Join Date
    Aug 2005
    Location
    Hertfordshire
    Posts
    1,537
    Thanks
    26
    Thanked
    18 times in 17 posts
    • Odc's system
      • Motherboard:
      • Gigabyte Z77-D3H
      • CPU:
      • Core i5 3570k
      • Memory:
      • 8GB Corsair XMS3
      • Storage:
      • 500GB Corsair MX100, 500Gb Samsung
      • Graphics card(s):
      • SLI Zotac GTX 560 Ti 448
      • PSU:
      • 750W Enermax
      • Case:
      • CoolerMaster Centurion 5
      • Operating System:
      • Windows 7 Professional x64
      • Monitor(s):
      • Dell SP2309W
      • Internet:
      • BT Infinity 2

    Re: Basic C Question :(

    I found the problem, you have to declare

    char testString[] = "Hello";

    instead.


    Hexus Trust = Odesi

  3. #3
    Banhammer in peace PeterB kalniel's Avatar
    Join Date
    Aug 2005
    Posts
    29,585
    Thanks
    1,585
    Thanked
    3,007 times in 2,443 posts
    • kalniel's system
      • Motherboard:
      • Gigabyte Z390 Aorus Ultra
      • CPU:
      • Intel i9 9900k
      • Memory:
      • 32GB DDR4 3200 CL16
      • Storage:
      • 1TB Samsung 970Evo+ NVMe
      • Graphics card(s):
      • nVidia GTX 1060 6GB
      • PSU:
      • Seasonic 600W
      • Case:
      • Cooler Master HAF 912
      • Operating System:
      • Win 10 Pro x64
      • Monitor(s):
      • Dell U2311H
      • Internet:
      • rubbish

    Re: Basic C Question :(

    Yup. Strings are arrays of characters, and declaring arrays has slightly different nomenclature to declaring simple variables (and in your case you're initialising it at the same time as declaring it).

  4. #4
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,147
    Thanks
    798
    Thanked
    2,151 times in 1,407 posts

    Re: Basic C Question :(

    Sounds like you fixed the problem, but just make sure your happy with the difference between a pointer and an array as I'm assuming this is school work!

    That said the amount of times I screw something up because i'll be using < rather than >.....
    throw new ArgumentException (String, String, Exception)

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked
    0 times in 0 posts

    Re: Basic C Question :(

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

    int main()
    {
    int i, totalCharacter, totalE;
    char txt[301];

    // Ask user to enter his/her sentence
    printf("Type your sentence: ");
    gets(txt);

    // 1) Total characters in the sentence (spaces included)
    for(totalCharacter = 0; totalCharacter < txt[totalCharacter]; totalCharacter++);
    printf("Your sentence has %i characteres (including any spaces).\n\n", totalCharacter);

    // 2) Total e's in the sentence
    totalE = 0;
    for(i = 0; i < totalCharacter; i++){
    if(txt[i] == 'e')
    totalE++;}
    printf("There are %i 'e' in your sentence.\n\n", totalE);

    // 3) Sentence with words reversed (INCOMPLETED!)
    for(i = 0; i < totalCharacter; i++)



    // 4) Sentence with letters AND words reversed (INCOMPLETED!)
    for(i = 0; i < totalCharacter; i++)


    system("PAUSE");
    return 0;
    }

  6. #6
    cat /dev/null streetster's Avatar
    Join Date
    Jul 2003
    Location
    London
    Posts
    4,138
    Thanks
    119
    Thanked
    100 times in 82 posts
    • streetster's system
      • Motherboard:
      • Asus P7P55D-E
      • CPU:
      • Intel i5 750 2.67 @ 4.0Ghz
      • Memory:
      • 4GB Corsair XMS DDR3
      • Storage:
      • 2x1TB Drives [RAID0]
      • Graphics card(s):
      • 2xSapphire HD 4870 512MB CrossFireX
      • PSU:
      • Corsair HX520W
      • Case:
      • Coolermaster Black Widow
      • Operating System:
      • Windows 7 x64
      • Monitor(s):
      • DELL U2311
      • Internet:
      • Virgin 50Mb

    Re: Basic C Question :(

    you what?

  7. #7
    Registered+
    Join Date
    Aug 2013
    Posts
    21
    Thanks
    1
    Thanked
    0 times in 0 posts

    Re: Basic C Question :(

    Rovina, can you please clarify your question? I don't get what you want to say?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Basic RAID question
    By DougMcDonald in forum PC Hardware and Components
    Replies: 20
    Last Post: 17-08-2007, 04:23 PM
  2. Basic Question
    By fhsieh in forum abit.care@HEXUS
    Replies: 4
    Last Post: 04-07-2007, 09:00 PM
  3. Not a very technical Question, but a Question none the less.
    By chip35 in forum Help! Quick Relief From Tech Headaches
    Replies: 6
    Last Post: 24-03-2007, 09:05 PM
  4. Basic n00b question
    By Skii in forum PC Hardware and Components
    Replies: 9
    Last Post: 27-10-2003, 06:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •