Question

Write a C program where user enter a string input, tokenizer the string using space character....

Write a C program where user enter a string input, tokenizer the string using space character. Calculate the lengh of string. Find word start. Find word end. Count words.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program

//preprocessor
#include <stdio.h>
//main function for executing the code
int main()
{
   //variable declaration
   int i,length=0,wordcount=0;
   //storing the string in a character array
    char str[]="today is sunday its a holiday";
    //running loop for accessing the each character in the string untill end of the string is encountered
    for(i=0;str[i] !='\0';i++){
       if(length==0){//printing the starting of the word
           printf("\nStart of word\n\"");
       }
       if(str[i]==' '){//if space character is encountered then count it as end of the word
               printf("\"\nEnd of word\n");//print the word has ended
               printf("\nStart of word\n\"");//starting new word
               wordcount++;//incrementing the counter for the words                  
       }
       else{
           printf("%c",str[i]);//print the character in the strings
       }
       length++;//count the length of the string
   }
   printf("\"\nEnd of word\n\n");//after encountering end of the file, print end of the word
   wordcount++;//increment word count as last word will not end by space instead by \0
  
   printf("String is: \"%s\"\n",str);//print the string used for tokenizing
   printf("Length of the string: %d\n",length);//print the length of the string
   printf("No of words: %d\n",wordcount);//print the number of word counted

    return 0;
}

Output:

E:\Program\C & C++1StringToken.exe Start of word today End of word Start of word End of word Start of word sunday End of word Start of word its End of word Start of word End of word Start of word holiday End of word String is: today is sunday its a holiday Length of the string: 29 No of words: 6

Add a comment
Know the answer?
Add Answer to:
Write a C program where user enter a string input, tokenizer the string using space character....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT