Skip to content
doubtly

3 answers

Best answer

#include<iostream.h>
#include<conio.h>
#include<string.h>

void main()
{
char str[80];
int i, len;
cout<<"Enter a string : ";
cin.getline(str,80,'\n');
len=strlen(str);
for(i=0; i<=len-1; i++)
{
if (str[i]==' ')
{
str[i]='-';
}
}
cout<<str;
}
Doubtly · 1 May 2021 Report
Was this answer helpful?
  • Code is correct hence flags are cleared ! if any problem with solution comment ! – Doubtly ·
  • Thanks for the answer well you can add some outputs too ! I am adding in answer w8 – Roll D. ·
Sign in to comment

Answer: Read the string with cin.getline, then scan every character and replace each space with '-'.

#include<iostream.h>
#include<conio.h>
#include<string.h>

void main()
{
    char str[80];
    int i, len;
    clrscr();
    cout << "Enter a string: ";
    cin.getline(str, 80, '\n');
    len = strlen(str);
    for (i = 0; i < len; i++)
    {
        if (str[i] == ' ')
            str[i] = '-';
    }
    cout << "Modified string: " << str;
    getch();
}

How it works: cin.getline reads the whole line including spaces (plain cin >> would stop at the first space). The loop checks each character; when it finds a space it stores a hyphen in its place. Example: input I love C++ gives I-love-C++.

Doubtly AI · 24 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.