I am kinda new to world of c programming, and i have been assigned a problem that is written as follows: Write a procedure that counts the number or words in a string. Write a program to test your new procedure. I am kinda lost on this completely. Any help on how to define the number of words by the amount of spaces. How do i define a word? Where do i want to start. I figure it will involve some form of looping and a counter. Any help will be greatly appreciated

This sounds like a simple string. I know nothing about C, but I know you'll need a string or function for this.

Look through any docs or manuals you can find. It would be something like count$() but dont rely on me, im a newbie too :D

thanks, i think i figured it out.

thanks, i think i figured it out.

great. Can you post the code, so others can learn?

I had the same assignement 1 month ago ;)
Here's the code :

#include <stdio.h>
#include <string.h>
#define SIZE 100

int count(char vector[],int nr);

int main()

{  
     int i,string_size,x; 
     char string[SIZE]; 
     printf("Type a text: ");
     gets(string);
     string_size=strlen(string);
     x=count(string,string_size);
     printf("Number of words: %d\n",x);

     return 0;
}

int count (char vector[],int nr)

{
    int gasit=0,i; 
    for(i=0;i<nr;i++)
    {
        if(vector[i]>='a' && vector[i]<='z') gasit++;
          if(vector[i]>='A' && vector[i]<='Z') gasit++;
    }
   
    return gasit;

    }

@ Windsurfer : If there's something you don't understand ,feel free to reply

Good luck

int count (char vector[],int nr)

{
    int gasit=0,i; 
    for(i=0;i<nr;i++)
    {
        if(vector[i]>='a' && vector[i]<='z') gasit++;
          if(vector[i]>='A' && vector[i]<='Z') gasit++;
    }
   
    return gasit;

}

I think there is a better way of doing this, just use the [search]isalpha( )[/search] function which checks whether the given character is an alphabet or not. You require ctype.h header file to use this function.

Hope it helped, bye.

int count (char vector[],int nr)

{
    int gasit=0,i; 
    for(i=0;i<nr;i++)
    {
        if(vector[i]>='a' && vector[i]<='z') gasit++;
          if(vector[i]>='A' && vector[i]<='Z') gasit++;
    }
   
    return gasit;

}

I think there is a better way of doing this, just use the [search]isalpha( )[/search] function which checks whether the given character is an alphabet or not. You require ctype.h header file to use this function.

Hope it helped, bye.

Very nice tip :)
You were right , it's much easier and simple with isalpha()
Here's the new version:

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main()
{
    char string[100];
    int i,found=0;
    
    printf("Type a string : ");
    gets(string);

    for(i=0;i<strlen(string);i++)
        if( isalpha(string[i]))
                    found++;
    printf("Number of characters: %d\n",found);

    return 0;
}

Thanks ~s.o.s~

Member Avatar for iamthwee

ha ha, gets(string);
Newbie.

ha ha, gets(string);
Newbie.

Ah... looks like it missed my eye.

@Eko:
Hey there buddy, normally its a bad programming practice to use the function gets( ) since it does no bound checking i.e. even if you declare your string to be consisting of 10 characters, nothing prevents you from entring 100 characters. This will result in buffer overflow and writign to memory that doenst belong to you which in the end leads to corruption.

You are better off using fgets( ).

char buffer[BUFSIZ] = { '\0' } ; // set character array to all null chars
printf( "Enter string: " ) ;
fgets( buffer, BUFSIZ, stdin ) ;  //

[search]fgets( )[/search]
The above is a much safer way of accpeting strings or a matter of fact any input from the user.

Dont forget to remove the newline character which is present at the string end.

Offtopic :
@ iemtwhee I don't know what u find so funny ; if you are so "advanced" , go write a c programming book or something :lol:

@~s.o.s~ Thanks for the support,i heared about fgets(),but our teachers at the university want us to use the gets() function and if u don't do it their way,than is the highway :lol:

Member Avatar for iamthwee

Offtopic :
@ iemtwhee I don't know what u find so funny ; if you are so "advanced" , go write a c programming book or something :lol:

@~s.o.s~ Thanks for the support.But our teachers at the university want us to use the gets and if u don't do it their way,than is the highway :lol:

I ain't writing a book but I am getting thru quite a few utility classes. He he.

Offtopic :
Thanks for the support,i heared about fgets(),but our teachers at the university want us to use the gets() function and if u don't do it their way,than is the highway

No need to as such go against them but a little extra knowledge is always good. Just keep in mind never to use gets( ) unless you are doing a University assignment since then it would really be the highway for you....

The thing with gets() function at university will be only in first semester,to "accomodate".
In the second semester will have a better teacher
Thanks

Very nice tip :)
You were right , it's much easier and simple with isalpha()
Here's the new version:

#include <stdio.h>
#include <ctype.h>
#include <string.h>
 
int main()
{
    char string[100];
    int i,found=0;
 
    printf("Type a string : ");
    gets(string);
 
    for(i=0;i<strlen(string);i++)
        if( isalpha(string[i]))
                    found++;
    printf("Number of characters: %d\n",found);
 
    return 0;
}

Thanks ~s.o.s~

Yeah this works.But my problem is that I am not allowed to use islapha function since its not been taught yet. I have to do my assignment without using functions from cstring.h:rolleyes: :sad: So can someone tell me how to do it without that? i tried this

#include<stdio.h>
#include<string.h>
void main()
{
char string[80];
char *s;
int count=0;
int len;
printf("Enter string\n");
gets(string);
len=strlen(string);
printf("%d",len);
for(s=string;*s!='\0';s++)
 {
 if(*s==' ')
  {
  count++;
  }
 else
  break;
 }
printf("Number of words in string %d",count);
}

Yeah this works.But my problem is that I am not allowed to use islapha function since its not been taught yet. I have to do my assignment without using functions from cstring.h:rolleyes: :sad: So can someone tell me how to do it without that?

Well, my friend. You were almost there:

#include<stdio.h>
#include<string.h>
int main(void)
{
    char string[80];
    char *s;
    int count=0;
    int len;
    
    printf("Enter string: ");
    gets(string);
    len=strlen(string);
    printf("The length of the string is %d characters long\n",len);
    
    for(s = string; *s != '\0'; s++)
     {
         if(*s == ' ')
              count++;
    }

    printf("Number of words in string %d",count+ 1); /* add 1 to count to account the last word */

    getchar();
    return(0);
}

Compare this one with yours and see where your code is different.
I want to point out a few things.

main() is always an int so it should be written

int main(void)

in your case.
Your code format needs better spacing and indenting. And there were problems with the {} brackets. In fact you didn't need the else statement and the break.

I am not allowed to use islapha function since its not been taught yet.

Each character has a numerical value. Therefore you can simply compare characters. For example, to see if the character ch is a digit, you can use: if (ch >= '0' && ch <= '9') If true, it's a digit.

Thanks so much.It worked. Yeah I know that it is always int main with a return 0 and about indentation, i was so harassed that time that I didn't care. Thanx anyways.

Thanks so much.It worked. Yeah I know that it is always int main with a return 0 and about indentation, i was so harassed that time that I didn't care. Thanx anyways.

But we do. And so will your boss... ;)

You're welcome!

Well, my friend. You were almost there:

#include<stdio.h>
#include<string.h>
int main(void)
{
    char string[80];
    char *s;
    int count=0;
    int len;
    
    printf("Enter string: ");
    gets(string);
    len=strlen(string);
    printf("The length of the string is %d characters long\n",len);
    
    for(s = string; *s != '\0'; s++)
     {
         if(*s == ' ')
              count++;
    }

    printf("Number of words in string %d",count+ 1); /* add 1 to count to account the last word */

    getchar();
    return(0);
}

Compare this one with yours and see where your code is different.
I want to point out a few things.

main() is always an int so it should be written

int main(void)

in your case.
Your code format needs better spacing and indenting. And there were problems with the {} brackets. In fact you didn't need the else statement and the break.

how would i get this code to line count also?

how would i get this code to line count also?

Define what a line is and then read upto that. Keep count of how many
times you reach that definition, and your there.

Hi Eko,
I have a question...what if we don't have the function strlen() available with us.. I mean what functionality runs behind it... Requires a little hint.............

Thanks and Regards
Nidhi Sharma

I had the same assignement 1 month ago ;)
Here's the code :

#include <stdio.h>
#include <string.h>
#define SIZE 100
 
int count(char vector[],int nr);
 
int main()
 
{  
     int i,string_size,x; 
     char string[SIZE]; 
     printf("Type a text: ");
     gets(string);
     string_size=strlen(string);
     x=count(string,string_size);
     printf("Number of words: %d\n",x);
 
     return 0;
}
 
int count (char vector[],int nr)
 
{
    int gasit=0,i; 
    for(i=0;i<nr;i++)
    {
        if(vector[i]>='a' && vector[i]<='z') gasit++;
          if(vector[i]>='A' && vector[i]<='Z') gasit++;
    }
 
    return gasit;
 
    }

@ Windsurfer : If there's something you don't understand ,feel free to reply

Good luck

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
int i,c=0, whitespace=0;
char line[5],character;clrscr();
printf("Enter the sentence and press <Return> in the end\n");
do
{
character=getchar();line[c]=character;c++;
}while(character!='\n');
c=c-1;line[c]=' ';
for(i=0;i<=c;i++){
character=line[i];
if (isspace(character)!=0) whitespace++;
}
printf("Words=%d\n",whitespace);
getch();
}
commented: Showing up 3 YEARS LATE, with unformatted code (did you even read the intro threads), and using void main makes you a total waste of space -4

I am kinda new to world of c programming, and i have been assigned a problem that is written as follows: Write a procedure that counts the number or words in a string. Write a program to test your new procedure. I am kinda lost on this completely. Any help on how to define the number of words by the amount of spaces. How do i define a word? Where do i want to start. I figure it will involve some form of looping and a counter. Any help will be greatly appreciated

i need someone to teach me about how to delete white spaces..im still a first year student.

I'm not sure why you want to delete white spaces??

This is a two year old thread, so it's best to start a new thread, and perhaps tell us what you are trying to do, in more detail.

If you will show me your code, I will show you mine. ;) I call the function "compress", and sometimes "boogie_left". All it does is just take a string of chars, and delete any spaces from it.

i need a program in c the program is their is a file in that first we have take 0-9 letters in that we count how many no of a,b,c...,z then we take 1-10 we do the same we do the same sequence until the end of the file.

i need a program in c the program is their is a file in that first we have take 0-9 letters in that we count how many no of a,b,c...,z then we take 1-10 we do the same we do the same sequence until the end of the file.

Sorry to say this but we're not a coding service meaning we don't provide code but only helps members from theirs

If you need further help start a new thread (instead of replying to an old one) if your troubled by a problematic code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.