Better Passwords

#include <iostream>

using namespace std;

int main() {
	char A[40],B[40];
	cin>>A;
	if(A[0]>='a' && A[0]<='z') B[0]=A[0]-32;
	else B[0]=A[0];
	for(int i=1,j=1; i<40; i++,j++){
		if(A[i]=='s') B[j]='$';
		else if(A[i]=='i') B[j]='!';
		else if(A[i]=='o'){
			B[j]='(';
			j++;
			B[j]=')';
		}
		else if(A[i]=='\0'){
			B[j]='.';
			j++;
			B[j]='\0';
		}
		else B[j]=A[i];
	}
	cout<<B;
	return 0;
}

It shows runtime error, but I am unable to find the problem. Please help.

You need a condition in your loop to make it stop when you reach the end of the input string.

1 Like

Please Help

(Python 3.7)

A=input()
new=β€˜β€™
if A[0]!=β€˜s’ and A[0]!=β€˜i’ and A[0]!=β€˜o’:
new=new+A[0].upper()
for i in range(len(A)):
if A[i]==β€˜s’:
new=new+β€˜$’
elif A[i]==β€˜i’:
new=new+β€˜!’
elif A[i]==β€˜o’:
new=new+β€˜()’
else:
new=new+A[i]
new=new+β€˜.’
print(new)

Please help me with this one.

(Python 3.7)

A=input()
if A[0]!=β€˜s’ and A[0]!=β€˜i’ and A[0]!=β€˜o’:
A=A.replace(A[0], A[0].upper())
A=A.replace(β€˜s’,β€˜$’)
A=A.replace(β€˜i’,β€˜!’)
A=A.replace(β€˜o’,β€˜()’)
A=A+β€˜.’
print(A)

It’s showing a wrong answer.

A=A.replace(A[0], A[0].upper())

I think you got wrong hoe the replace() function works.
Try different ways of capitalizing the 1st character.
It’s showing WA cause it is producing WA.
for inputs like aba, your code will capitalize every a word and will show AbA.

1 Like

Okay! That’s gonna be a pain.

What’s the output for β€œoption” here? β€œOpt!()n.” or β€œ()pt!()n.”?

#include <stdio.h>
int main()
{
    char str[100];
    int i,j,len;
    len=0;
    gets(str);
    if(str[0]>=97&&str[0]<=122)
    {
        printf("%c",str[0]-32);

    }
    else if(str[0]=='s')
    {
    printf("$");
    }
    else
    {
        printf("%c",str[0]);
    }
    

    for(i=0; str[i]!='\0'; i++)
    {

        len++;

    }
    for(j=1; j<=len; j++)
    {


        if(str[j]=='s')
        {
            printf("$");

        }
        else if(str[j]=='i')
        {
            printf("!");

        }
        else if(str[j]=='o')
        {
            printf("()");

        }

        else
        {
            printf("%c",str[j]);
        }


    }
    printf(".");
    return 0;


}

This code runs perfectly in my IDE and yields the correct shown output,but in Toph it shows a question mark at the end,please help

What is the problem with this code ?
I ran more than 20 words and it perfectly show me the expected answer on my IDE.
**** Python *****

word = []
a = input().lower()
if len(a) < 16:

for x in a:
    if x == 's':
        x = '$'
    if x == 'i':
        x = '!'
    if x == 'o':
        x = '()'
    word.append(x)
if word[0] == '$':
    word[0] = 's'
if word[0] == '!':
    word[0] = 'i'
if word[0] == '()':
    word[0] = 'o'

for y in word:
    if y == word[0]:
        print(word[0].upper(), end='')
    else:
        print(y, end='')

print(β€˜.’)

/******************************************************************************

                          Online C++ Compiler.
           Code, Compile, Run and Debug C++ program online.

Write your code in this editor and press β€œRun” button to compile and execute it.

*******************************************************************************/

#include

using namespace std;

int main()
{

string password;
cin>>password;
//password[0]=password[0]-32;

char betterpassword[16];

int i=0,j=0;
for(j=0;j<password.length();j++){
if(password[j]=='s'){
betterpassword[i]='$';
i++;
}    
else if(password[j]=='i'){
betterpassword[i]='!';
i++;
}    
else if(password[j]=='o'){
betterpassword[i]='(';
i++;
betterpassword[i]=')';
i++;
}    
else {
betterpassword[i]=password[j];
i++;
}

}
if(password.length()==j){
betterpassword[i]=β€˜.’;
}
//unsophisticated
cout<<betterpassword;

return 0;

}
why

s = str(input())
if len(s) < 16:
    u = s.replace(s[:1], s[:1].upper())
    u = u.replace('s', '$')
    u = u.replace('i', '!')
    u = u.replace('o', '()')
    print(u + '.')

This gets the β€˜wrong answer’ in the 5th test case. I don’t know what’s wrong
with it.

This problem is created for newbies in the first place. So, I would appreciate it if you do not post solves in the community. Just give clues.

u = s.replace(s[:1], s[:1].upper())

@Aurjon the replace function doesn’t work this way. It takes a string and replaces it with another string. This, just change the first character separately.

Uncomment this line maybe?

#include<stdio.h>
#include<string.h>

int main()
{
    int i,l,j;
    char s[30],n[31];
    gets(s);
    l = strlen(s);
    n[0]='A'+(s[0]-'a');
    j = 1;
    for(i=1;i<=l-1;i++)
    {
        if(s[i]=='s')
        {
            n[j]='$';
        }
        else if(s[i]=='i')
        {
            n[j]='!';
        }
        else if(s[i]=='o')
        {
            n[j]='(';
            j++;
            n[j]=')';
        }
        else
        {
            n[j]=s[i];
        }
        j++;
    }
    printf("%s.\n",n);
  
    return 0;
}

It shows wrong answer after the 5th cases. Where is the problem?

A = input()

r = A.replace(A[0], A[0].upper()).replace("s", "$").replace("i", "!").replace("o", "()")

print(r + ".")

Why my code is showing an error in 5th testcase ???

Help me what is wong here !!!

A = input()
li1 = ('s', 'i', 'o')
li2 = ('$', '!', '()')

for i in A:
	if i ==  A[0]:
		A = A.replace(i, i.upper())
	elif i == li1[0]:
		A = A.replace(i, li2[0])
	elif i == li1[1]:
		A =  A.replace(i, li2[1])
	elif i == li1[2]:
		A =  A.replace(i, li2[2])
if '.' in A:
	output = f"{A}"
else:
	output = f"{A}."
print(output)`Preformatted text`

#include <stdio.h>
#include <string.h>
int main ()
{ char a[16];
gets(a);
int i= strlen (a);

a[0] =a[0] - 32;
for (int j=0;j<i;j++)
{

if (a[j] == β€˜s’)
{ a[j] = β€˜$’ ;
}
if (a[j]==β€˜i’)
{ a[j]= β€˜!’;
}

}

for (int b=0;b<i;b++)
{ if ( a[b]==β€˜o’)
{ a[b] = β€˜(’;
for (int e=i+1;b+2<e;e–)
{
a[e] = a[e-2] ;
}
for (int t=b+2;t<=i;t++)
{ a[t] =a[t+1] ;
}

a[b+1]= β€˜)’ ;

}
}

printf(β€œ%s.”,a);
return 0;
}
image
why this code shows error? it seems alright in my ide

@touhidurrr
Can you please help me out,how can I replace o with (),because each time I am having wrong answer and it only replace β€˜(’ this character

Do you use C or C++ Language?

1 Like