Looping through characters in a string

Posted on 2007-12-02
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!
Question by:nahumba
  • 2
  • 2
  • 2
  • +1

Expert Comment

ID: 20392925
while (char!= 0)

Expert Comment

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

Expert Comment

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 :

and then strlen to get the length of the line :
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 30

Accepted Solution

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')

Open in new window


Author Comment

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.

LVL 53

Expert Comment

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

Expert Comment

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
An API detour question 7 94
nested if statement in excel help 4 37
WMIC Command LIne 5 46
Fill Null values 5 28
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
The goal of this video is to provide viewers with basic examples to understand and use structures 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.

821 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