My Homework is Some certain punctuations, period (.), comma (,), colon (:), semi-colon (;), question mark (?), and exclamation mark(!), should be followed by a space. For example, the following strings should be corrected because there is no space after the above punctuations. (There might be some other punctuations which need a space after; however, given punctuations (.,:;?!) will suffice) I saw you playing soccer.You are such a good player. I saw you playing soccer. You are such a good player. Hello!How are you?Fine thanks,and you. Hello! How are you? Fine thanks, and you.
I am trying to make space after all punctations but i getting error
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char string[200];
scanf("%s", string[200]);
char string1[200] = " ";
for(int i=0;i<strlen(string);i ){
if( s[i] !='.' && ',' && ';' && ':' && '!' && '?'){
string1 = string1 string[i];
}
else{
string1= string1 string[i] " ";
}
}
string = string1;
printf("new string : %s", string);
return 0;
}
can someone help me or can someone convert my c code to C
using namespace std;
int main()
{
string s;
cout<<"Please enter the string"<<endl;
cin>>s;
cout<<"Input string:"<<s<<endl;
string s1="";
for(int i=0;i<s.length();i )
{
if(s[i]!='.'&&s[i]!=','&&s[i]!=';'&&s[i]!=':'&&s[i]!='!'&&s[i]!='?')
s1=s1 s[i];
else
s1=s1 s[i] " ";
}
s=s1;
cout<<"Updated string:"<<s<<endl;
return 0;
}
I am trying to make space but cant in C lang can someone figured out ?
CodePudding user response:
Operator
does not work on operands of type char[]
. You should just copy the characters to string1
:
for(int i = 0, i1 = 0, string_length = strlen(string); i < string_length; i )
{
string1[i1 ] = string[i];
if (string[i] is one of the punctuation characters)
string1[i1 ] = ' ';
}
I moved strlen
call to initialization part of the for
loop so that it's called only once, instead of many times.
But pay attention to one minor problem with this code. If the punctuation character is already followed by a space then the resulting string will have two spaces. If you are not fine with this then you would have to add more code.