So the assignment is this: Problem C: Practice Strings (lastnames.c)(8 points)
Read in n, then n lastnames, and check to see if the first in the list is ever repeated again.
Sample Run #1
Enter n, followed by n Last names (each last name must be a single word):
5 Reagan Bush Clinton Bush Obama
First name in list is not repeated.
Sample Run #2
Enter n, followed by n Last names (each last name must be a single word):
4 Bush Clinton Bush Obama
First name in list is repeated.
I can get the first two names to compare, but I can't figure out how to compare the first to whatever is in the second string array. I don't want to post my code in the event that someone searches this and copies mine. I'll send it to you though. Any help would be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
// initializing character strings
char last[25], first[25];
// initializing number of names, and the index for the number of names
int index, n;
// read in the number
printf("Enter n, followed by n Last names (each last name must be a single word) :\n");
scanf("%d", &n);
scanf("%s", first);
for (index = 0; index < n; index++)
scanf("%s", last);
if (strcmp(last, first)== 0)
{
printf("First name in list is repeated.\n");
}
else
{
printf("First name in list is not repeated.\n");
}
return 0;
}
In your forloop the lastvariable is overwritten every time which means that you are only comparing the first input to the last name in the next sequence of n lastnames.
If you want to count the number of repetitions, use a counter within the for-loop.
int nRepetitions = 0;
/* ... read the numbers and the first string
(therefore index should start with 1)... */
for (index = 1; index < n; index++) {
scanf("%s", last);
if (strcmp(last, first) == 0) {
nRepetitions++;
}
}
#include <stdio.h>
#include <string.h>
int main(void){
char last[25], first[25];
int index, n, repeated = 0;
printf("Enter n, followed by n Last names (each last name must be a single word) :\n");
scanf("%d", &n);
scanf("%s", first);
for(index = 1; index < n; index++){ //index = 1 : aleady input first
scanf("%s", last);
if(strcmp(last, first)== 0)
repeated = 1;
}
if(repeated)
printf("First name in list is repeated.\n");
else
printf("First name in list is not repeated.\n");
return 0;
}
Your code is incorrect..
For loop last has just 1D character array and which is overwritten everytime.
So Use a 2D array instead..
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
// initializing character strings
char first[25];
// initializing number of names, and the index for the number of names
int index=0, n;
// read in the number
printf("Enter the number of last names :\n");
scanf("%d", &n);
printf("Enter the first name \n");
scanf("%s", first);
char last[n][25];
for (int i = 0; i < n; i++)
scanf("%s", last[i]);
for(int i=0;i<n;i++)
{
if (strcmp(last[i], first)== 0)
index++;
}
if(index==0)
printf("First name not repeated\n");
else
printf("First name repeated %d times", index);
return 0;
}
Related
I should take strings from user WORDSIZE times.
And I should do this with recursion.
In this code any loop, any static variables, and string library function not allowed.
So I send count and n variable as a pointer.
#include <stdio.h>
#define WORDSIZE 5
/* I accepted wordsize as number of the words to be delete. To delete "hello"and"world", wordsize should be 2 */
int take_words(char (*array)[WORDSIZE], int *m, int *cnt)
{
if((*m) == 0)
return 0;
else
{
scanf("%s", array[*cnt]);
getchar();
printf("(1)This word will be entered %s\n", array[*cnt]);
//This printf works correctly.
(*m)--;
(*cnt)++;
take_words(array, m, cnt);
}
}
int main()
{
char (*words)[WORDSIZE];
int n=WORDSIZE, count=0;
printf("Enter the words one by one\n");
take_words(words, &n, &count);
for(int i=0; i<WORDSIZE; i++)
printf("(2)Occured array's %ith term: %s\n",i, words[i]);
}
Problem is that, althoug printf(1) works correct, printf(2) in main doesn't give the correct result. Here is the output:
Enter the words one by one
hello
(1)This word will be entered hello
darkness
(1)This word will be entered darkness
my
(1)This word will be entered my
old
(1)This word will be entered old
friend
(1)This word will be entered friend
(2)Occured array's 0th term: hellodarknmy
(2)Occured array's 1st term: darknmy
(2)Occured array's 2nd term: my
(2)Occured array's 3rd term: old
(2)Occured array's 4th term: friend
Array couldn't create right. How could I hanlde this?
So I've got an assignment where my program asks the brand (10 letters), model (10 letters), age (1986 - 2019) and cost (positive real number) of 10 cars and then wants the program to check which car is the oldest and to print out it's brand and model. I don't have a problem with the first part but with the second part.
The code is:
//First part
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define C 10
#define M 11
int main(void)
{
char brand[C][M];
char model[C][M];
int year[C];
float cost[C];
int i, len1, len2, min;
for(i=0; i<C; i++){
printf("Car %d\n", i+1);
do{
printf("Brand: ");
scanf("%s", brand[i]);
len1 = strlen(brand[i]);
} while(len1<0 || len1>10);
do{
printf("Model: ");
scanf("%s", model[i]);
len2 = strlen(model[i]);
} while(len2<0 || len2>10);
do{
printf("Year: ");
scanf("%d", &year[i]);
} while(year[i]<1986 || year[i]>2019);
do{
printf("Cost: ");
scanf("%d", &cost[i]);
} while(cost[i]<=0);
}
//Second part
year[0] = min;
for(i=0; i<10; i++)
if(year[i] < min){
min = year[i];
printf("\nThe oldest car is %s %s\n", brand[i], model[i]);
}
For some reason it either prints out gibberish in the place of brand[i] or if I lose the columns of the if statement prints out all the car brands and their models, where I only want the oldest one.
Aside from scanf not being recommended there are some problems with this code, first when you read the brand and model you do:
do{
printf("Brand: ");
scanf("%s", brand[i]);
len1 = strlen(brand[i]);
} while(len1<0 || len1>10);
The problem here is that you first write the string to brand[i] and then check if it's too long, but you have already written it into the array so if the string is longer than your space you already have a buffer overflow. Limit the size you can read with scanf using scanf("%10s, brand[i]) or better yet use fgets(brand[i], sizeof(brand[i]), stdin).
Next in the second part you use min without initializing it, and you overwrite the content of year[0] with it. You probably wanted something like:
min = 2020; // or a number that will be bigger than all your cars anyway
int older = 0;
i = 0;
for(i=0; i<C; i++){ // Use C here, you have it might as well use it instead of magic numbers
if(year[i] < min){
older = i;
min = year[i];
}
}
printf("\nThe oldest car is %s %s\n", brand[older], model[older]);
but bare in mind that this solution will print multiple cars if they are the oldest ones and have the same year
I must write a program in C that allows an user to say how many words they want to enter in a string and then I must sort those words based on their vowel number(the word with more vowels is first and the one with the least vowels is last - if two words have the same number of vowels then leave them in the order as they have appeared). For example:
string - "Aaaa Bbbbbbb B CD home Aaa BB A poke"
Sorted string - "Aaaa Aaa home poke A Bbbbbbb B CD BB"
I know how to write the first part, but for the sort part I have no idea. Can someone help me with that please?
EDIT: Currently I have this code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#pragma warning (disable: 4996)
int main(){
char string1[20], string2[100] = { '\0' };
int N, i;
do{
printf("Enter the number of words you want to enter in the string: ");
scanf("%d", &N);
if (N < 2){
printf("You must enter at least two words.\n");
printf("Enter the number of words you want to enter in the string: ");
scanf("%d", &N);
}
} while (N < 2);
for (i = 0; i < N; i++){
printf("Enter a word: ");
scanf(" %[^\n]", string1);
if (strlen(string2) == 0)
strcpy(string2, string1);
else {
strcat(string2, " ");
strcat(string2, string1);
}
}
printf("%s\n", string2);
return 0;
}
You will want to create an array of structs that hold a pointer to the word and then the number of vowels in each word. Something like:
struct vowelcnt {
char *word;
int numvowels;
}
Then sort the array of structs (descending order based on numvowels. Then simply loop through the sorted structs outputting the word which would give you the words sorted in order of the number of vowels contained. Hope that helps.
I know that I can use
scanf("%d %d %d",&a,&b,&c):
But what if the user first determines how many input there'd be in the line?
You are reading the number of inputs and then repeatedly (in a loop) read each input, eg:
#include <stdio.h>
#include <stdlib.h>
int main(int ac, char **av)
{
int numInputs;
int *input;
printf("Total number of inputs: ");
scanf("%d", &numInputs);
input = malloc(numInputs * sizeof(int));
for (int i=0; i < numInputs; i++)
{
printf("Input #%d: ", i+1);
scanf("%d", &input[i]);
}
// Do Stuff, for example print them:
for (int i=0; i < numInputs; i++)
{
printf("Input #%d = %d\n", i+1, input[i]);
}
free(input);
}
Read in the whole line, then use a loop to parse out what you need.
To get you started:
1) Here is the manual page for getline(3):
http://man7.org/linux/man-pages/man3/getline.3.html
2) Some alternatves to getline:
How to read a line from the console in C?
3) Consider compressing spaces:
How do I replace multiple spaces with a single space?
4) Use a loop for parsing. You might consider tokenizing:
Tokenizing strings in C
5) Be careful and remember that your user could enter anything.
#include <conio.h>
#include <stdio.h>
main()
{
int a[100],i,n_input,inputs;
printf("Enter the number of inputs");
scanf("%d",&n_input);
for(i=0;i<n_input;i++)
{
printf("Input #%d: ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n_input;i++)
{
printf("\nInput #%d: %d ",i+1,a[i]);
}
}
/*
_______________This program is in C Programming Language_______________
We have to directly enter all the elements in one line giving spaces between them. Compiler will automatically ends the for loop I have used and assign the value to their respective variables or array indexes. Below program and output will give you better understanding.
*/
#include <stdio.h>
int main()
{
//taking no of inputs from user
int len;
printf("Enter the number of inputs you want to enter : ");
scanf("%d", &len);
int i;
//defined an array for storing multiple outputs
int arr[100];
//included a printf statement for better understanding of end user
printf("Enter the inputs here by giving space after each input : ");
/*here is the important lines of codess for taking multiple inputs on one line*/
for (i=0;i<len;i++)
{
scanf("%d", &arr[i]);
}
printf("Your entered elements is : ");
for (i=0;i<len;i++)
{
printf("%d ", arr[i]);
}
}
/*
OUTPUT :
Enter the number of inputs you want to enter : 5
5 5 5 8 7
Your entered elements is : 5 5 5 8 7
*/
I am trying to learn C and here i got a program in which we have to take the input from the user as n number os strings, compare it and arrange it in a alphabetical order. After arranging them in a alphabetical order , i have to only print the last name which was occurring in the order.
Here is the code for the above problem:
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,m,n,len;
char a[50][50],temp[100];
char last ;
printf("Enter the number of elements you wish to order : ");
scanf("%d",&m);
printf("\nEnter the names :\n");
for (i=0;i<m;i++){
scanf("%s",a[i]);
}
for (i=0;i<m;i++){
for (j=i+1;j<m+1;j++) {
if (strcmp(a[i],a[j])>0) {
strcpy(temp,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],temp);
}
}
}
printf("\n\nSorted strings are : ");
for (i=0;i<m+1;i++){
printf("%s \n",a[i]);
}
return 0;
}
~
The Answer goes this way:
Enter the number of elements you wish to order : 4
Enter the names :
territory
states
hello
like
Sorted strings are : S$???
hello
like
states
territory
My question goes that why am i getting "S$???" and i want only the last word "territory should be printed out not all the names".
Can anyone let me know where am i going wrong? It will be a great help.
Thanks
Tanya
You are sorting m+1 strings with indexes [0..m]. But you input only m strings.
Your indexes should never go above m-1.
\Check this code
In your code you get input as 4
then
a[0]=territory
a[1]=states
a[2]=hello
a[3]=like
But your code runs upper loop at most 3 time then i=3
In next loop j=i+1 then j=4
but a[4]=not exists
Thats why "S$???" occurs
Solution:
#include<stdio.h>
#include<string.h>
int main() {
int i, j, m, n, len;
char a[50][50], temp[100];
char last;
printf("Enter the number of elements you wish to order : ");
scanf("%d", &m);
printf("\nEnter the names :\n");
for (i = 0; i < m; i++) {
scanf("%s", a[i]);
}
for (i = 0; i < m - 1; i++) { // m - 1 enough maximum i = 2;
for (j = i + 1; j < m; j++) { // j maximum j = i + 1 j = 3
if (strcmp(a[i], a[j]) > 0) {
strcpy(temp, a[i]);
strcpy(a[i], a[j]);
strcpy(a[j], temp);
}
}
}
printf("\n\nSorted strings are : "); // print all words in sorted order
for (i = 0; i < m; i++) {
printf("%s \n", a[i]);
}
printf("Last Word:\n");
printf("%s\n", a[m - 1]); // a[3] contains last name
return 0;
}
this was my program it worked for me in turbo c++
#include<conio.h>
#include<iosream.h>
#include<ctype.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
clrscr();
char name[100];
int c=0;
// to take the name including spaces
gets(name);
//this line is to print the first character of the name
cout<<name[0];
//this loop is to print the fist character which is there after every space
for(int l=0;name[l]!='\0';l++)
{
if(name[l]==' ')
{
cout<<"."<<name[l+1];
//l+1 is used because l is the space and l+1 is the character that we need
}
}
//this loop is to find out the last space in the entire sting
for(int i=0;name[i]!='\0';i++)
{
if(name[l+1]==NULL)
{
//here we fond the last space in the string
for(int j=i;name[j]!=' ';j--)
{
c=j+1;
}
// c=j+1 is used bacause we have already taken the first letter of the that word
//no need of taking it again
}
}
//this loop starts from the last space and the position(of space) is stored in k
for(int k=c;name[k]!='\0';k++)
{
cout<<name[k];
}
getch();
}
output:
anentt ranjan shukla
a.r.shukla