LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 08-21-2002, 01:52 AM   #1
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Rep: Reputation: 30
I did the following program and get blank line after pressing <ENTER>


Hi. Fairly new to C. I was wondering if anyone can explain to me why this didn't work .

#include <stdio.h>

#define BUFF 256

int main(void){

char name[BUFF];
int i;

puts("Enter your FULL name and press <ENTER>");
gets(name);

for(i=0; name[i]!= '\0'; i++){
if(name[i] == ' '){
i = i-1;
}
}

printf("Your FULL NAME is: %s\nThere are %d characters in your
full name\n", name, i);

return 0;

}

I understand that I SHOULD NOT use gets as even the compiler GCC gave me a warning about using it but It was a simple program for demonstration purpose and I don't understand why it didn't work. It was supposed to display FULL name and count characters in 'name' excluding white space but I just get a new cursor after pressing <ENTER> and a blank line

thanks.
 
Old 08-21-2002, 02:19 AM   #2
Malicious
Member
 
Registered: Jan 2002
Location: Galveston Island
Distribution: suse, redhat
Posts: 208

Rep: Reputation: 30
messing with loop counter

Code:
for(i=0; name[i]!= '\0'; i++){
   if(name[i] == ' '){
	i = i-1;
   }
}
This should work as long as there is no space in the input.

However, once you find a space in the input, you decrement the loop counter; the end of the loop increments it and you continue at the same "i" value forever. Messing with the loop counter is normally a no-no, but there are occasions when you will want to.
 
Old 08-21-2002, 02:43 AM   #3
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
oh!!! Cool!!! SO I see now. Once white space is hit 'i' goes down one and then is incremented and then goes down again and then up again etc. making an endless loop. Am I following you?

I should have maybe done it this way ..

int b=0;

for(i=0; name[i] != '\0'; i++){
if( name[i] == ' '){
b++;
}
}

/* now lets subtract white space from count of characteres */
i = i - b;


thanks alot malicious

Last edited by purpleburple; 08-21-2002 at 02:45 AM.
 
Old 08-21-2002, 03:08 AM   #4
Malicious
Member
 
Registered: Jan 2002
Location: Galveston Island
Distribution: suse, redhat
Posts: 208

Rep: Reputation: 30
You got it...

What about:
Code:
for(i=0; name[i] != '\0'; i++){
    if (name[i] != ' ') 
        b++;
}
and just print b?
 
Old 08-21-2002, 11:55 AM   #5
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
awesome ..... YEAH!!! that would make a little more sense ... and wouldn't that be a 'little' more efficient then the way I did it in program execution time??

Not that you'd ever notice on today's computer's or maybe even oldies ....


back to programming ......
 
Old 08-21-2002, 12:00 PM   #6
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
actually no ... the program's intent was to 'count' the characters that the user enters 'excluding' white space so I'd have to subtract b from i I would think ..

im new .......
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH: read every line in the files and use the line as parameters as another program tam3c36 Programming 10 12-07-2010 01:42 PM
Mandrake 10 doesnt install after pressing enter subliminal Mandriva 9 06-17-2004 03:04 PM
Blank line - grep. liguorir Linux - Software 1 04-24-2004 09:03 AM
Viewing program messages when program isn't run from command line? Locura Linux - Software 1 09-27-2003 08:19 AM
how do I print 'blank space' to standard out in C on same line? purpleburple Programming 7 08-18-2002 11:54 PM


All times are GMT -5. The time now is 03:36 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration