0
#include <iostream>
using namespace std;
int main(){
    char word[100];
    char count;
    int j=0;
    cout<<"Enter a word or a phrase"<<endl;
    cin>>word;
    cout<<endl<<word<<endl;
    j=sizeof(word);
    cout<<j;
}

What I want to do in the above program is to find out the length of the string(word) that was inputted by the user, but the above program just gives the size of the whole array which is 100.

  • 1
    Why do not use `std::string`? Then you can use `std::string::size()`. – Gary Strivin' Dec 27 '20 at 21:55
  • using arrays is part of the assignment. – Agitated_Mouse Dec 27 '20 at 21:57
  • 3
    Ok. Anyway, consider that [`using namespace std;` is considered a bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Gary Strivin' Dec 27 '20 at 21:59
  • @SaimBaloch `assignment` So I assume you are in some sort of school? If that is true, please consider reading [The top answer to this question](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions). Your attention is specifically drawn to "Asking about homework"/"Be aware of school policy". – Nikita Demodov Dec 27 '20 at 22:05
  • As a hint: One way would be,if you just iterate over the word array for example a for loop, checking for `word[i]` to be `== '\0' ` and then break, the value of `i` will be what you want. – HelpingHand Dec 27 '20 at 23:06

3 Answers3

3

The size of the array is, indeed, 100.

But you're looking for the number of characters set inside that array until the first null byte, i.e. the length of the "C-string" inside the array.

strlen does that.

You'd be much better off with a nice std::string though, if for no other reason than you currently perform no bounds checking, so if your user inputs text of 100 characters or more, you'll overflow your array. This is at best a nasty bug, and at worst a common security risk.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35
  • Note that with a character array and `strlen` all bets are off if the user inputs a string longer than 99 (100 - 1 for the terminator) characters. Overflow an array and the program behaviour [will get weird](https://en.cppreference.com/w/cpp/language/ub). – user4581301 Dec 27 '20 at 22:25
1
char word[100];

This is a c-style string. They are null-terminated -- That means they end with a \0. Example:

INPUT: Hello!
word: 'H', 'e', 'l', 'l', 'o', '!', '\0' // <-- NULL-TERMINATOR

These kinds of strings are not recommended in c++ and are usually only found in legacy code. Use std::string (#include <string>) and its length-function instead!

Nikita Demodov
  • 553
  • 5
  • 17
0
#include <iostream>
#include <string.h>
using namespace std;
int main(){
    char word[100];
    cout << "Enter a word or a phrase" << endl;
    cin >> word;
    cout<< strlen(word) <<endl;
}

EDIT: this is a right one, you need to include <string.h> and use strlen which will find the length according to the '/0' symbol which means the end of c_str.