Print the last string name in C - c

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

Related

Words frequency with number of letters (basic loop only)

I Want to write C program to print words length and their frequency by letters number with basic loops techniques. I could get the word length work but I stuck with frequency
(example: Do 2 not 3 judge 5 a 1 book 4 (had solved this))
there are # words with 1 letter
there are # words with 2 letter
etc...
#include <stdio.h>
int main(void) {
char word[30];
int i = 0,b=0,c=0,j=0,d=0;
printf("Please enter a word: ");
for (i = 0; i < 30 ; i++){
scanf("%s", word);
while (word[b]!='\0'){
b++;
}
printf("%s %d ", word, b);
b = 0;
}
return 0;
}
Your question wasn't completely clear. But from what I understood, you also wanted to print the number of times(frequency) a word of length 'l' is inputted by the user. So I will answer that :
You could just store the length of the word in an array that the user inputs. Once all the inputs are read, you can just print the frequency of each word length from the stored array
Refer the following code to understand what I meant :
#include <stdio.h>
int main(void) {
char word[30];
int i = 0,b=0,c=0,j=0,d=0;
int word_length_freq[30]={0}; //an array which will store the frequency of word length(all initialized to 0)
//eg. if word is "hello" it will increase count of word_length_freq[5] by 1
printf("Please enter a word: ");
for (i = 0; i < 3 ; i++){
scanf("%s", word);
while (word[b]!='\0'){
b++;
}
word_length_freq[b]++;
printf("%s %d ", word, b);
b = 0;
}
for(int i=1;i<30;i++){ //This will print the frequency of all words from length 1 to 30
printf("There are %d words of length %d\n",word_length_freq[i],i);
}
return 0;
}
I hope this solves your question !

segmentation fault (core dumped) gcc ubuntu

I was trying to make a simple function to make a group of number that user enters them, using pointer of pointer but i keep getting this error and its hard to tell where the problem is, if there any other option to use something else that tells me where the problem is instead of this weird error.
#include <stdio.h>
void BuildGroub(int** group,int* count){
int i=0;
int j;
printf("Enter the size of the group \n");
scanf("%d", &*count);
while(*count != 0){
printf("Enter the %d number of the group:\n", i);
j=0;
scanf("%d", &**(group+i));
while(**(group+i)!=**(group+j)){
j++;
}
if(j==i){
i++;
count--;
} else{
printf("you have already entered this number please try again: \n");
}
}
}
int main(){
int count;
int group[100];
int *groupptr = &group;
BuildGroub(&groupptr,&count);
for(int i=0;i<count;i++){
printf("%d, ", group[i]);
}
return 0;
}
With this question, you do not need to use double pointer. If you want to learn how to use the double pointer, you can google then there are a ton of examples for you, for example, Double Pointer (Pointer to Pointer) in C.
In BuildGroub you decrease the count pointer
if(j==i){
i++;
count--;
}
, but in the condition of while loop, you compare the value that count pointer points to. it seems strange.
while(*count != 0)
Even if you change count-- to (*count)--, it will decrease the number of elements that you enter to 0 when you get out of the while loop, then in main function:
for(int i=0;i<count;i++){} // count is equal to 0 after calling BuildGroub function if you use (*count--) in while loop.
You should use a temp value for while loop function, for example:
int size = *count;
while(size != 0){
...
if (i == j) {
i++;
size--;
}
}
You should use, for example, group[i] instead of *(group+i). It will be easier to read your code.
The code complete:
#include <stdio.h>
void BuildGroub(int* group,int* count){
int i=0;
int j;
printf("Enter the size of the group \n");
scanf("%d", count);
int size = *count;
while(size != 0){
printf("Enter the %d_th number of the group:\n", i);
j=0;
scanf("%d", &group[i]);
while(group[i] != group[j]) {
j++;
}
if(j==i){
i++;
size--;
} else{
printf("you have already entered this number please try again: \n");
}
}
}
int main(){
int count;
int group[100];
int *groupptr = group;
BuildGroub(groupptr,&count);
for(int i=0;i<count;i++){
printf("%d, ", group[i]);
}
return 0;
}
The test:
./test
Enter the size of the group
5
Enter the 0_th number of the group:
1
Enter the 1_th number of the group:
2
Enter the 2_th number of the group:
2
you have already entered this number please try again:
Enter the 2_th number of the group:
3
Enter the 3_th number of the group:
3
you have already entered this number please try again:
Enter the 3_th number of the group:
4
Enter the 4_th number of the group:
5
1, 2, 3, 4, 5,
If you want to use a double pointer, you need to change your function like this:
void BuildGroub(int** group, int* count) {
int i = 0;
int j;
printf("Enter the size of the group \n");
scanf("%d", &*count); //I think this is redundant but works.
while (*count != 0) {
printf("Enter the %d number of the group:\n", i);
j = 0;
scanf("%d", (*group + i)); //The content of group + i
while ( *( *group + i) != *(*group + j)) { //the content of the content
j++;
}
if (j == i) {
i++;
(*count)--; //The content decrement
} else {
printf("you have already entered this number please try again: \n");
}
}
}
But you have a big problem in main and it is because you are using the parameter count to decrement until zero inside the function. So when the function finish, count value is zero and you don't print anything... You need to change this, using a internal variable to make the count, and finaly, setting the parameter to be using in main.

Why should i use a space before %c while taking input into an array? [duplicate]

This question already has answers here:
what is the purpose of putting a space in scanf like this scanf(" %c",&ch) in place of scanf("%c",&ch)? [duplicate]
(6 answers)
Closed 6 years ago.
I have just written a program to reverse a word. I have first compiled the program as follows:
//This program will reverse a given word
#include <stdio.h>
int main()
{
int letters, i, j;
printf("Enter the number of letters in your word: ");
scanf("%d", &letters);
int word[letters];
printf("Enter %d Letters: ", letters);
for( i = 0; i < letters; i++){
scanf("%c", &word[i]);
}
for( j = i - 1; j >= 0; j-- ){
printf("%c", word[j]);
}
return 0;
}
Then, I inputted 5 to store in letters and the word "rubel" (ignore the inverted comma) to reverse. The expected output was "lebur". But unfortunately i got ebur. then i recompiled the code as below:
//This program will reverse a given word
#include <stdio.h>
int main()
{
int letters, i, j;
printf("Enter the number of letters in your word: ");
scanf("%d", &letters);
int word[letters];
printf("Enter %d Letters: ", letters);
for( i = 0; i < letters; i++){
scanf(" %c", &word[i]);
}
for( j = i - 1; j >= 0; j-- ){
printf("%c", word[j]);
}
return 0;
}
This time i got expected output which is "lebur". Now, my question is what was wrong before that i didn't get expected output and what have i just done by putting a space that this time i got the expected result. Thanks in advance.
In the first case word[0] became \n (you entered 5\n). In the second case the \n was skipped because you told scanf to skip whitespace characters (by ).

error in output of long number multiplication

I have written a code in c for long number multiplication but output is not being displayed on the IDE. Please can you point out the error in the given code. Also which language is more efficient for solving these type of problems?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10000
void main()
{
char ac[MAX];
char bc[MAX];
int a[MAX],b[MAX];
int mul[MAX];
int c[MAX];
int temp[MAX];
int la,lb;
int i,j,k=0,x=0,y;
long int r=0;
long int sum = 0;
la=strlen(ac)-1;
lb=strlen(bc)-1;
printf("Enter the first number : ");
scanf("%s",ac);
printf("Enter the second number : ");
scanf("%s",bc);
for(i=0;i<=la;i++){
a[i] = ac[i] - 48;
}
for(i=0;i<=lb;i++){
b[i] = bc[i] - 48;
}
for(i=lb;i>=0;i--){
r=0;
for(j=la;j>=0;j--){
temp[k++] = (b[i]*a[j] + r)%10;
r = (b[i]*a[j]+r)/10;
}
temp[k++] = r;
x++;
for(y = 0;y<x;y++){
temp[k++] = 0;
}
}
k=0;
r=0;
for(i=0;i<la+lb+2;i++)
{
sum =0;
y=0;
for(j=1;j<=lb+1;j++){
if(i <= la+j){
sum = sum + temp[y+i];
}
y += j + la + 1 ;
}
c[k++] = (sum+r) %10;
r = (sum+r)/10;
}
if (r==1)
{
c[k]=r;
}
j=0;
for(i=k-1;i>=0;i--){
mul[j++]=c[i];
}
for(i=0;i<j;j++)
{
printf("%d",mul[i]);
}
}
You calculate the length of the input strings before assigning the strings:
la=strlen(ac)-1;
lb=strlen(bc)-1;
printf("Enter the first number : ");
scanf("%s",ac);
printf("Enter the second number : ");
scanf("%s",bc);
If you instead do it the other way around the program actually does something:
printf("Enter the first number : ");
scanf("%s",ac);
printf("Enter the second number : ");
scanf("%s",bc);
la=strlen(ac)-1;
lb=strlen(bc)-1;
Your second problem is in the last part of your code:
for(i=0;i<j;j++)
{
printf("%d",mul[i]);
}
You're incrementing j instead of i, it should be:
for(i=0;i<j;i++)
{
printf("%d",mul[i]);
}
Another little thing, this isn't really clear what it does to the uninitiated:
a[i] = ac[i] - 48;
if you write it like this it's easier to understand:
a[i] = ac[i] - '0';
Use remove those la and l assignment and put it after scanning the strings like below.
printf("Enter the first number : ");
scanf("%s",ac);
printf("Enter the second number : ");
scanf("%s",bc);
la=strlen(ac)-1;
lb=strlen(bc)-1;
for(i=0;i<=la;i++){
a[i] = ac[i] - 48;
}
for(i=0;i<=lb;i++){
b[i] = bc[i] - 48;
}
Even in the last part of your code there is an error.Inside the loop you have used for(i=0;i<j;j++) replace it with for(i=0;i<j;i++).
I would also suggest you to change the size of temp array to 2*MAX+2.Because suppose somebody enters a number of 8000 digits then temp wont be able to store these many digits if the size is restricted to MAX only.
you can use java and python languages for doing same. In Java there is separate class known as BigInteger.
Scanner sc=new Scanner(System.in);
BigInteger b1,b2,b3;
String num1,num2;
System.out.println("Enter the first integer");
num1=sc.next();
System.out.println("Enter the second integer");
num2=sc.next();
b1=new BigInteger(num1);
b2=new BigInteger(num2);
b3=b1.multiply(b2);
System.out.println("The product of "+b1+" and "+b2+" is "+b3);

String Compare issues in C

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

Resources