Interview Question
Country: United States
Java Implementation:
public static void modifyString()
{
Scanner sc = new Scanner(System.in);
String sInput = sc.nextLine();
int iChanges = 0;
sInput = sInput.replace('a', '+');
sInput = sInput.replace('A', '+');
sInput = sInput.replace('E', '+');
sInput = sInput.replace('e', '+');
sInput = sInput.replace('i', '+');
sInput = sInput.replace('I', '+');
sInput = sInput.replace('o', '+');
sInput = sInput.replace('O', '+');
sInput = sInput.replace('u', '+');
sInput = sInput.replace('U', '+');
for(int i=0; i< sInput.length(); i++)
{
if(sInput.charAt(i) == '+')
{
iChanges++;
}
}
System.out.println("Modified String: " + sInput);
System.out.println("Changes made: " + iChanges);
}
-> for(i=0;i<;80;i++)
There is an extra ; between < and 80. Helps to keep the code more readable by sticking to best styling practices:
for (int i = 0; i < 80; i++) {
// Some stuff here
}
When you are parsing in a string with scanf, an array "decays" into a pointer to its first element, so scanf("%s", string) is equivalent to scanf("%s", &string[0]). On the other hand, scanf("%s", &string) passes a pointer-to-char[80], but it points to the same place.
One more thing, the %s conversion specifier causes scanf to stop at the first whitespace character. So you need to use a conversion specifier like scanf("%[^n], &string) to read to the next end-line character.
Also remember that "A" is != 'A' ... first one is a string of type const char *, the second is a char array with a single ascii element.
This code works now:
// main.c
// BufferString
//
// Created by Admin on 3/1/16.
// Copyright (c) 2016 Admin. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char * argv[]) {
char buffer[80];
uint i = 0, counter = 0;
// Ask user to input a string and read it into a buffer char array.
printf("Enter a string of max 80 characters. \n");
scanf("%80[^\n]", buffer);
printf("You entered: %s \n", buffer);
long len = strlen(buffer);
printf("The length of the string is: %ld \n", len);
char *outputString = malloc(len + 1 + 1 ); // one for extra char, one for trailing zero
for (i = 0; i < len; i++)
{
if(buffer[i] == 'a' || buffer[i] == 'e' || buffer[i] == 'i' || buffer[i] == 'o' || buffer[i] == 'u'
|| buffer[i] == 'A' || buffer[i] == 'E' || buffer[i]== 'I' || buffer[i]== 'O' || buffer[i]== 'U')
{
// printf("%c", buffer[i]);
// printf("\n");
strcat(outputString,"+");
counter++;
} else {
// printf("%c", buffer[i]);
// printf("\n");
outputString[i] = buffer[i];
}
// Need to terminate the char array with a NULL char to assert string.
outputString[i + 1] = '\0';
}
printf("The modified string is: %s \n", outputString);
printf("In total there was %d vowels. \n", counter);
return 0;
}
-> for(i=0;i<;80;i++)
There is an extra ; between < and 80. Helps to keep the code more readable by sticking to best styling practices:
for (int i = 0; i < 80; i++) {
// Some stuff here
}
When you are parsing in a string with scanf, an array "decays" into a pointer to its first element, so scanf("%s", string) is equivalent to scanf("%s", &string[0]). On the other hand, scanf("%s", &string) passes a pointer-to-char[80], but it points to the same place.
One more thing, the %s conversion specifier causes scanf to stop at the first whitespace character. So you need to use a conversion specifier like scanf("%[^n], &string) to read to the next end-line character.
Also remember that "A" is != 'A' ... first one is a string of type const char *, the second is a char array with a single ascii element.
This code works now:
// main.c
// BufferString
//
// Created by Admin on 3/1/16.
// Copyright (c) 2016 Admin. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char * argv[]) {
char buffer[80];
uint i = 0, counter = 0;
// Ask user to input a string and read it into a buffer char array.
printf("Enter a string of max 80 characters. \n");
scanf("%80[^\n]", buffer);
printf("You entered: %s \n", buffer);
long len = strlen(buffer);
printf("The length of the string is: %ld \n", len);
char *outputString = malloc(len + 1 + 1 ); // one for extra char, one for trailing zero
for (i = 0; i < len; i++)
{
if(buffer[i] == 'a' || buffer[i] == 'e' || buffer[i] == 'i' || buffer[i] == 'o' || buffer[i] == 'u'
|| buffer[i] == 'A' || buffer[i] == 'E' || buffer[i]== 'I' || buffer[i]== 'O' || buffer[i]== 'U')
{
// printf("%c", buffer[i]);
// printf("\n");
strcat(outputString,"+");
counter++;
} else {
// printf("%c", buffer[i]);
// printf("\n");
outputString[i] = buffer[i];
}
// Need to terminate the char array with a NULL char to assert string.
outputString[i + 1] = '\0';
}
printf("The modified string is: %s \n", outputString);
printf("In total there was %d vowels. \n", counter);
return 0;
}
I took the liberty of modify the above example a bit. There is no need for an output buffer. We only have to remember that "+" is a string i.e. two bytes '+; and '\0' and 'x' is one byte, therefore we can simply replace the current buffer[i] with '+' in case of vowel. I kept the copyright notice.
// main.c
// BufferString
//
// Created by Admin on 3/1/16.
// Copyright (c) 2016 Admin. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char * argv[]) {
char buffer[80];
int i = 0, counter = 0;
// Ask user to input a string and read it into a buffer char array.
printf("Enter a string of max 80 characters. \n");
scanf("%80[^\n]", buffer);
printf("You entered: %s \n", buffer);
long len = strlen(buffer);
printf("The length of the string is: %ld \n", len);
for (i = 0; i < len; i++){
if(buffer[i] == 'a' || buffer[i] == 'e' || buffer[i] == 'i' || buffer[i] == 'o' || buffer[i] == 'u'
|| buffer[i] == 'A' || buffer[i] == 'E' || buffer[i]== 'I' || buffer[i]== 'O' || buffer[i]== 'U'){
buffer[i] = '+';
counter++;
}
}
printf("The modified string is: %s \n", buffer);
printf("In total there was %d vowels. \n", counter);
return 0;
}
I did this and there is an error, can someone, please help me :(
- Dominykas1.Katilius March 01, 2016