Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main(){
char buffer[80];
char word[80];
char word2[80];
scanf("%s",word);
strcpy(buffer, word);
int i;
int counter=0;
 
for(i=0;i<80;i++)
{
    word2[i]="";
}
 
 
for(i=0;i<strlen(buffer);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")
//   if(strcmp(buffer[i],"a")==0)
   {
       strcat(word2,"+");
       counter++;
   }else{
       strcat(word2,buffer[i]);
   }
 
}
 
 
printf("The modified string is %s",word2);
printf("in total there was %d vowels.",counter);
 
 
return 0;
}

I did this and there is an error, can someone, please help me :(

- Dominykas1.Katilius March 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
}

- diksha2207 March 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

-> 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;
}

- asadeghi March 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

-> 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;
}

- asadeghi March 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}

- jovanovic.dragoslav June 30, 2016 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More