Solved

Looping through characters in a string

Posted on 2007-12-02
7
1,600 Views
Last Modified: 2012-05-05
Hi all,
Trying to play a little bit with C programming.
I want to loop through the number of characters in a string.
My user will enter some string, and then I want to loop through the string and put the number characters in an int var.

I'm just having problem with the conditions of the loop statement.
 char = getchar();
  while (char!= '\n') {
...
...
}
The problem is that I'm getting an endless loop here.
How can this be solved?

Thank you!
0
Comment
Question by:nahumba
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20392925
while (char!= 0)
0
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20392929
Sorry, ignore this.
Next time I will read entire post :-)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20392935
char is a reserved keyword in C - you can't use it as variable name.

I'd also suggest to use gets to read an entire line :

        http://www.cplusplus.com/reference/clibrary/cstdio/gets.html

and then strlen to get the length of the line :

        http://www.cplusplus.com/reference/clibrary/cstring/strlen.html
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 30

Accepted Solution

by:
SteveGTR earned 500 total points
ID: 20392941
You should be reading the characters in the loop.

Could do this:


char c;
int cnt=0;
 
while ((c=getchar()) != '\n')
  cnt++;

Open in new window

0
 

Author Comment

by:nahumba
ID: 20392943
I don't know why,
but I still get an endless loop...

For example:
right after my:
printf ("Enter some text here:  ");
the loop begins, but will never end.

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20392946
Can you show your complete code ? As well as the text you enter ?
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20392961
Here's a nice example that does exactly what you want:

http://sole.infis.units.it/C/NewCcourse/subsubsection3_10_2_1.html
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question