Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Write a program that asks the user to input a sentence and prints the number of letters and words in the sentence?

0 votes
asked Oct 31, 2017 by Amira Ibrahim (120 points)

4 Answers

0 votes
answered Nov 5, 2017 by Ruturaj Khandare
#include <stdio.h>

int main()
{
    int i=0,countl=0,countw=0;
    char my1[200],a;
    printf("enter sentence ");
  
   gets(my1);
    while(my1[i]!='\0')
    {
        countl++;
        if(my1[i]==' ')
        countw++;
         printf("%c ",my1[i]);
        i++;
       
    }
       printf("\n no of letter = %d",countl);
       countw++;
    printf("\n no of words = %d",countw);
    

    return 0;
}
commented Nov 24, 2017 by anonymous
There is a bug in this program, what if two contiguous spaces are there?
0 votes
answered Nov 28, 2017 by fathamd (180 points)
import java.util.Scanner;

public class demo {

    public static void main(String[] args) {

    Scanner input =new Scanner(System.in);
    int sum=0, len=0;
    System.out.println("please input the sequence");
    String str= input.nextLine();
    String[] st=str.split(" ");
    for(int i= 0;i<st.length;i++) {
      sum=sum+st[i].length();        
    }
    len=st.length;
    System.out.println("number of chars are:" + sum);
    System.out.println("number of words are:" + len);
    }

}
0 votes
answered May 23, 2020 by dinesh sen (290 points)
s=str(input('write a sentence : '))
sp=0
n=len(s)
for x in range(0,n):
    if (s[x])==" ":
        sp=sp+1
print('total words are : {0}'.format(sp+1))
print('total letters are : {0}'.format(n-sp))
0 votes
answered May 24, 2020 by Jha Mrityunjay (140 points)
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string s;
    int i,x=0,y=0;
    getline(cin,s);
    int n=s.length();
    for(int i=0;i<n;i++)
    {
        if(s[i]!='/0') x++;
        if(s[i]==' ') y++;
    }
    cout<<x<<endl<<(y+1);
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...