#include <stdio.h>
#include<string.h>
int main(void)
{
int n,u = 0;
printf("Enter length of word ");
scanf("%d",&n);
char word[n*2];
printf("Enter the String \n");
scanf("%s", word);
printf("string that u have entered: %s\n",word);
int i = n - 1, j = n;
while(i >= 0)
{
printf("word[i-1] %c\n",word[i]);
word[j] = word[i];
printf("i %d\n",i);
printf("j %d\n",j);
i--;
j++;
}
printf("reversed word stored at the last in the same array - %s\n",word);
}
when we give the length 3 the output doesn't give any garbage value in output but when I give greater than 3 its starts to give garbage value in the output. (yeah I know we have to add '\0' at the end of the array to stop but for length 3 without that, it works.)
Here, you have declared the word as char array and alloted the size of array. So,when you are printing via indexing of array as %c --it gives the correct result. But,at the end,when you try to print the whole 'word' char array, you used %s . Now, what %s does is : when you indicate to a string, it stores the address of the first element and keep printing the value stored in those addresses in sequential manner untill it finds the null value. So,you better use a loop to print values stored in 'word' array using %c or you can convert your char array 'word' into string & then use %s to print values inside 'word'.
Related
I want to write a program that, with the help of pointers, takes the names of a number of people from the input and receives one character from the input for each row of the array and prints the number of repetitions of that character in the array line.
#include <stdio.h>
int main () {
int n, i, j;
char str[30][30];
printf ("how many names?:");
scanf ("%d", &n);
int m = 0;
for (i = 0; i < n; i++)
{
char ch, *s;
scanf ("%s", str[i]);
gets (str[i]);
s = str[i];
ch = getche ();
while (*s) if (*s++ == ch) m++;
printf ("%d\n", m);
}
return 0;
}
This program is incomplete and wrong. I want to know how we can take a letter from each of the names separately from the input and get the number of repetitions after giving the name entries to the program.
input: 2** leonardo ** peter ** o ** p /////
output:
how many names?
2
please enter 1th name:
leonardo
please enter 2th name:
peter
Number of repetitions Which letter of the name 1 do you want?
o
2
Number ofrepetitions Which letter of the name 2 do you want?
p
1
You can define a struct say containing the name and the count of all characters.
struct name_st
{
char name[30];
int count[26];
};
Create an array of the struct objects like: struct name_st s[30];
Read each name in the for loop like scanf("%s", s[i].name);
For each name, after reading you can parse the name character by character and update the count for each character like s[i].count[s[i].name[j] - 'a']++;
Then in another for loop, read the character you want to display the count for in the ith name, and simply output the count for that particular character like printf("%d", s[i].count[c - 'a']);
#include <stdio.h>
#include <string.h>
int main(void)
{
char alpha[26] = { '0' };
char nl;
while (alpha != '0'){
scanf("%c", &alpha);
scanf("%c", &nl);
printf("the character is %c\n", alpha);
}
int i, j, size;
for (i=0;i<size;i++){
for (j=i;j<size;j++){
if (alpha[i]<alpha[j]){
Swap(&alpha[i], &alpha[j]);
}
}
}
printf("%s", alpha);
return 0;
}
I'm getting an error "comparison between pointer and integer" in my while loop. I'm wanting to read in each letter of the alphabet from a text file and stop when it reaches a "0" at the end of the list. It's then going to sort alphabetically starting with z,y,x.. etc. How else could I write this so it stops at "0" without using an integer?
Thanks for the help
alpha [26] is an array of 26 chars, with your while loop you always overwrite the first element of the array (scanf("%c", &alpha); overwrites the first element of alpha in every iteration of the loop ) , the entire code will not work. to access the elements of the array you can either use pointers or indexes, indexes are easier, try a for loop
int i;
for (i = 0; i<26 ; i++)
{
if(alpha[i] != '0')
{
scanf("%c", &alpha[i]);
printf("the character is %c\n", alpha[i]);
}
}
C: scanf to array
for using pointers to access array see Can i use pointer in scanf to take input in an array?
I have given a task to generate a sub string for the given input in C. The code as follows.
#include<stdio.h>
int main(){
char a[1000];
char *sub;
int startFrom = 0;
int endAt = 0;
printf("Enter the String: ");
scanf("%s", a);
printf("Start From? ");
scanf("%d", &startFrom);
printf("End At? ");
scanf("%d", &endAt);
sub = &a[startFrom];
a[endAt] = '\0';
printf("%s\n", sub);
return 0;
}
The code however works fine, but what will happen to the rest of the characters in the array?
The rest of the array remains the same; it's just that you changed one of the characters in the array to null('\0'). So if you try to access any other character after (or before) the a[endAt] character, you would be able to do so.
Check it out Your code with some extra at Ideone.com.
However as you can see, when you try to print the original array, it would be printed only till the first '\0' character.
Anyone know how to search (with luck) for specific elements in an array? I'v tried about everything - except the correct way.
My two corresponding char-arrays looks something like this:
char array1[10][10]={"Alpha","Bravo","Charlie","Delta","Alpha2"}; //room for some more here
char array2[10][10]={"123456","234567","345678","456789","567890"}; //room for some more here
I can print them all by the regular for-loop:
for (i=0;i<10;i++){
printf("%s %s \n", &array1[i], &array2[i]);
}
But let's say I want do the same loop, and only print the two elements starting with 'A' (the first and last one). I thought something like this would work
do {
for (i=0;i<10;i++){
printf("Name: %s\nDate: %s\n\n", &array1[i][x], &array2[i]);
}
} while (x=='A');
That's the same for-loop, except I put the extra [x] behind &array1 to tell my 'x' have to match the first character in each element of array1 (like it will only do the for loop while (x=='A') - which is the first letter in the elements I want.
But it doesn't do any good..
Anyone have a better solution? I've tried other variants of the for loop also, without luck. I'm just stuck now...
#include <stdio.h>
int main(void) {
char array1[10][10]={"Alpha","Bravo","Charlie","Delta","Alpha2"}; //room for some more here
char array2[10][10]={"123456","234567","345678","456789","567890"}; //room for some more here
int i;
for (i=0;i<10;i++){
if(array1[i][0]=='A') //add this statement in your code
printf("%s %s \n", &array1[i], &array2[i]);
}
return 0;
}
using while you are checking x=='A' it will compare with the ASCII value of A. The condition is wrong.
you can use
while((strncmp(array,"A",1)) == 0)
this will check the first character of the string.
except I put the extra [x] behind &array1 to tell my 'x' have to match the first character in each element of array1
That's not quite how array subscripts work. As you have found, array1[i] will refer to the ith string in array1. You can then access the xth character in array1[i] by indexing into the string, i.e. array1[i][x].
You don't actually assign x anywhere, so you'll end up with undefined behaviour. What you want to do is extract the first character of array1[i] (i.e. element zero), so the following is possible:
char x = '\0';
do {
for (i=0;i<10;i++){
x = array1[i][0];
printf("Name: %s\nDate: %s\n\n", &array1[i][x], &array2[i]);
}
} while (x=='A');
You'll then notice that this probably doesn't give the behaviour you want. It'll loop over all 10 strings, then perform the x == 'A' check. You should move that inside the loop instead, e.g.
char x = '\0';
for(i = 0; i < 10; i++)
{
x = array1[i][0];
if(x == 'A')
printf("%s %s \n", &array1[i], &array2[i]);
}
I am learning C, and the problem I am running into is if I iterate through the string up to and including strlen(), I am expecting to get the null character (nothing) printed, but instead, I just keep getting '.' — whereas if I print array[strlen(array)], I get nothing (null character). What is going on?
#include <stdio.h>
#include <string.h>
#include <cs50.h>
int main(void)
{
char *mystring;
printf("Enter a string: ");
mystring = GetString();
//Prints '.' for mystring[i] when i = strlen(mystring)
for (int i = 0, j = strlen(mystring); i <= j; i++){
printf("The %dth character is: %c.\n", i+1, mystring[i]);
}
//Skips the line printing nothing ('\0')
printf("The last char is %c\n", mystring[strlen(mystring)]);
return 0;
}
You are actually printing this dot every time (right after %c): "The %dth character is: %c.\n". When your input is the null character %c won't print a thing since it's null and all you see is the dot afterwards.
For \0 nothing will be printed. But you have . after %c in printf statement-
printf("The %dth character is: %c.\n", i+1, mystring[i]);
Due to that ., when printing \0 it will print only .(For null it wont print anything).
printf("The %dth character is: %c\n", i+1, mystring[i]); //remove that . and try!