Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

Vote on your favorite hash algorithm here!

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-10-2007, 04:18 AM
Emir Emir is offline
Newbie
 
Join Date: Dec 2007
Posts: 1
Credits: 0
Rep Power: 0
Emir is on a distinguished road
Unhappy Newbie questions regarding inputing a sentence

Hello everyone,

I've got this school assignment that I must do, but I have certain problems with it.

(I'm programming use the programing language C, btw)

[Please do not flame me, I'm new and I'm probably asking some very newbie questions ]

The assignment is as follows:
1) I am to make a small program that in which the user is to input some text and the program outputs the same text, but removes 'white spaces' (tabs \t, new lines \n, double spaces, etc).

2) the second assignment is basically the same this as the first one, accept the user inputs a string that represents C programing language code and removes all the comments.

The problem is I don't know how to do that. We've been introduced to the %s which inputs a string the user enters, but it stops on the first space and doesn't go further. I know that, but how do I enable the program to accept the whole thing, with white spaces?

For example, my program should do the following:
user input: This is a /n sample /t code .
program output: This is a sample code.

(don't worry about my program exceeding the array limit, it's not an issue here)

Alright, now here's my code:

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

int main()
{
    char niz[50];
    int i=0;

    printf ("String plz \n");
    scanf ("%s", niz);
    getchar();

    for (i=0; i<50; i++)
    {
        if ( niz[i]=='\0')
        break;

        printf ("%c", niz[i]);
    }

    printf ("\n TEST %c", niz[5]);
    return 0;
}
Thanks for everyone in advance!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 12-10-2007, 09:45 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 2,535
Last Blog:
wxWidgets is NOT code ...
Credits: 919
Rep Power: 28
WingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the rough
Default

Try using the getline() function.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-21-2007, 11:34 AM
Freedom Doc Freedom Doc is offline
Newbie
 
Join Date: Dec 2007
Posts: 8
Credits: 0
Rep Power: 0
Freedom Doc is on a distinguished road
Default

Quote:
Originally Posted by WingedPanther View Post
Try using the getline() function.
The OP said he was using C. There is no getline() in C.
(If the OP is cool with using C++ he should say so).

To the OP: if you just need to read a sentence into a string, try fgets:

fgets(buff, 80, stdin);

would read 80 chars from stdin, or up to end of line or end of file, whatever comes first.
What it reads goes into buff, which should be declared
char buff[81];
If you want to read such a string from a file, say FILE *f then just say
fgets(buff, 80, f);
assuming f has been opened.

Last edited by Freedom Doc; 12-21-2007 at 11:51 AM. Reason: Needed to say more
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-29-2007, 06:21 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 607
Last Blog:
Programs Under the Hoo...
Credits: 195
Rep Power: 11
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default

There's a simpler way to do it. In both cases, just use scanf (fscanf for the file), but set the delimiter to the newline character. In other words, your format string should be "%[^\n]s".

Last edited by dargueta; 12-29-2007 at 06:28 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
need help writing prog to convert english sentence to pirate lingo dalearyous Java Help 11 05-07-2007 08:40 AM


All times are GMT -5. The time now is 09:01 AM.

Contest Stats

Xav ........ 1357.94
MeTh0Dz|Reb0rn ........ 1083.85
WingedPanther ........ 919.18
marwex89 ........ 906.86
morefood2001 ........ 903.18
John ........ 902.37
Brandon W ........ 789.89
chili5 ........ 312.39
Steve.L ........ 264.99
dcs ........ 240.34

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 83%

Ads