To put it bluntly, as it stands, I am not very good at coding at this current time. I am really trying to get these functions completed, I am just having trouble with the execution on some of them.
Basically, there are 10 functions ( 6 of which I have finished? ) that need to be created.
There is an int main() but, besides a string that needs to be fixed, the int main() does not need to be touched.
I will post the program (mostly so you can see my work) in addition to the int main() so that if someone wants to check the intent, they can see it. Even though I have posted the whole program, the only ones that I really want to focus on are the makeUpper, and the functions that have nothing in them because I really don't understand how to copy and edit a string into another array.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
void makeUpper( char orig[], char result[] )
{
/*
int i = 0;
while ( str[i] != '\0' )
{
if ( str[i] >= 'a' && str[i] <= 'z' )
str[i] = str[i] - 32;
k++;
}
*/
}
int countDigit( char str[] )
{
int i;
int count = 0;
int len;
len = strlen(str);
for ( i = 0; i < len; i++ )
{
if ( i >= '0' && i <= '9' )
count++;
}
return count;
}
int onlyLetters( char orig[], char letters[] )
{
}
int countOdd( int A[], int N )
{
int i;
int odd = 0;
for ( i = 0; i < N; i++ );
{
if ( A[i] % 2 != 0 )
odd++;
}
return odd;
}
int sumList( int A[], int N )
{
int i;
int sum = 0;
for ( i = 0; i < N; i++ );
sum = sum + A[i];
return sum;
}
void addToEach( int A[], int N, int val )
{
int i;
for ( i = 0; i < N; i++ )
A[i] + val;
printf("%d", A[i]);
}
void copyNumList( int orig[], int N, int result[] )
{
}
// populate list A with N items, random in range L to H
void fillNumList( int A[], int N, int L, int H )
{
}
// print the list, 10 items per line
void printList( int A[], int N )
{
int i;
for ( i = 0; i < N; i++ )
printf("%d ", A[i]);
printf("\n");
}
void rept( int n, char c )
{
int i;
for ( i = 0; i < n; i++ )
printf("%c", c);
printf("\n");
}
int main()
{
char line[100], other[100];
int i, len;
printf("Phrase: ");
fgets( line, 100, stdin );
// fix the string....
// ...
// really, do this
// ...
rept(10,'-');
printf("Orig: ##%s##\n", line);
rept(10,'-');
makeUpper( line, other );
printf("toupper: %s\n", other);
rept(10,'-');
i = countDigit( line );
printf("%d digits\n", i);
rept(10,'-');
len = onlyLetters( line, other );
printf("only letters: %s\n", other );
printf(" new len %d\n", len);
int nums[30], vals[30];
int many = 19;
rept(5, '-');
fillNumList( nums, many, 3, 11 );
printList( nums, many );
rept(5, '-');
i = countOdd( nums, many );
printf("%d odd values\n", i);
rept(5, '-');
i = sumList( nums, many );
printf("%d is sum\n", i);
rept(5, '-');
copyNumList( nums, many, vals );
printf("Copy\n");
printList( vals, many );
rept(5, '-');
addToEach( vals, many, 4 );
printf("Add 4\n");
printList( vals, many );
rept(5, '-');
printf("Orig\n");
printList( nums, many );
rept(5, '-');
return 0;
}
To answer the one almost-well-defined question (regarding makeUpper):
First, str is not a variable. You have orig and result, and haven't declared str anywhere.
Now, if str were orig, you'd be correctly uppercasing the characters in the original string. However, the task is to leave the original string alone and make an uppercased copy. The former is called "destructive operation" (as the original contents are not preserved); the latter, a "non-destructive operation" (for obvious reasons). Here are the changes needed to make your function non-destructive:
void makeUpper( char orig[], char result[] )
{
int k = 0;
do
{
if ( orig[k] >= 'a' && orig[k] <= 'z' )
result[k] = orig[k] - 32;
else
result[k] = orig[k];
} while ( orig[k++] != '\0' );
}
Notice that I iterate and test on orig, but assign to result.
onlyLetters should be similar; but you will need two index variables, not just k, since you will want to progress through orig and result at different pace (you'll only want to copy a character when it's a letter, and you'll only want to progress through result when you do a copy - as opposed to orig, which you'll always consume one character per loop).
As you have not stated what your misunderstanding is about copyNumList and fillNumList, I cannot give you any help there.
EDIT: I forgot about the terminator (and didn't test it); thanks Tom Karzes! Because the zero also needs to be copied, your loop is changed from while (condition) { ... } to do { ... } while (condition) - instead of testing whether we're at end and then doing things if we're not, we'll do the thing first, and only afterwards ask if we're done. This also requires the increment to be done after the test, so k++ got moved. If this is too weird for you, you can get the same effect like this, by explicitly adding the terminator after the loop is done:
void makeUpper( char orig[], char result[] )
{
int k = 0;
while ( orig[k] != '\0' )
{
if ( orig[k] >= 'a' && orig[k] <= 'z' )
result[k] = orig[k] - 32;
else
result[k] = orig[k];
k++;
}
result[k] = '\0';
}
I'm new to programming.Im learning from CS50. I'm not sure on how to define this code as a function.I want the function to return key[n].And how to call the function?
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, string argv[])
{
int n = strlen(argv[1]);
int key[n];
int k =0;
for( int i = 0; i < strlen(argv[1]); i++)
{
if(islower(argv[1][i]) != 0)
{
for(int j = 97; j < argv[1][i]; j++)
{
k++;
}
key[i] = k;
k = k*0;
}
if(isupper(argv[1][i]) != 0)
{
for(int j = 65; j < argv[1][i]; j++)
{
k++;
}
key[i] = k;
k = k*0;
}
}
}
The function can look for example the following way as it is shown in the demonstrative program below.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int * f( const char *s )
{
int *key = NULL;
size_t n = strlen( s );
if ( n != 0 && ( key = calloc( n, sizeof( int ) ) ) != NULL )
{
for ( size_t i = 0; i < n; i++ )
{
unsigned char c = s[i];
if ( islower( c ) ) key[i] = c - 'a';
if ( isupper( c ) ) key[i] = c - 'A';
}
}
return key;
}
int main( int argc, char * argv[] )
{
if ( argc == 2 )
{
int *key = f( argv[1] );
if (key != NULL)
{
// some other code
free( key );
}
}
return 0;
}
A function is a block of code with an "in" and "out". You define a function to receive values from the outside with syntax like int key (int example){} and pass in values to your function call with key(1);.
Your main() is also a function. The values that main() receives are being used in your code right now.
Your own function (which you define outside of main()) can be done in many ways, but the easiest would be to simply keep almost all of the same code within main() while figuring out how to transport what main() received into yours.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#pragma warning(disable:4996)
#define SIZE 100
int main(void){
char str[SIZE];
char str2[SIZE];
int i;
int len;
gets(str);
len = strlen(str);
for (i = 0; str[i] != NULL; i++) {
if (str[i] != ' '){
str2[i] = str[i];
}
}
for (i = 0; i < len; i++){
printf("%c", str2[i]);
}
return 0;
}
It returns the following error:
What is the problem?
I just want to copy some elements in str to str2 without spaces, but when I run, it has got some weird letters.
You need two index variables
one to go through str
one to tell where to write next to str2
code:
len = strlen(str);
int j;
for (i=0, j=0 ; str[i] != '\0' ; i++) {
if (str[i] != ' '){
str2[j++] = str[i]; // store to str2 and increment j
}
}
Then store a final \0 to str2 at index j
str2[j] = '\0';
Finally, to print the result, you can do that one shot
printf("%s\n", str2);
instead of printing one char at a time.
For starters these headers
#include <stdlib.h>
#include <windows.h>
can be removed because neither declaration from the headers is used in the program.
The function gets is unsafe and is not supported by the C Standard any more. Instead use standard C function fgets.
When str is copied in str2 you have to use separate index to access characters in str2 because some characters from str are not copied. Otherwise the array str2 will contain gaps. As result you can not use the previous value of the variable len with the array str2.
Also it is desirable not to copy any other white space characters.
The program can look the following way
#include <stdio.h>
#include <ctype.h>
#define SIZE 100
int main(void)
{
char str[SIZE];
char str2[SIZE];
fgets( str, sizeof( str ), stdin );
const char *p = str;
char *p2 = str2;
do
{
if ( !isspace( ( unsigned char )*p ) )
{
*p2++ = *p;
}
} while ( *p++ );
for ( p2 = str2; *p2; ++p2 )
{
printf( "%c", *p2 );
}
return 0;
}
Its output might be
Hello World
HelloWorld
If you do not study yet pointers then the program can look like
#include <stdio.h>
#include <ctype.h>
#define SIZE 100
int main(void)
{
char str[SIZE];
char str2[SIZE];
fgets( str, sizeof( str ), stdin );
size_t i = 0, j = 0;
do
{
if ( !isspace( ( unsigned char )str[i] ) )
{
str2[j++] = str[i];
}
} while ( str[i++] );
for ( j = 0; str2[j]; ++j )
{
printf( "%c", str2[j] );
}
return 0;
}
This is a program for string reverse without using the strlen() and strrev() functions. Please help me regarding this code: the length function shows 11 but the reverse function is not working.
#include <stdio.h>
#include <conio.h>
int length(char*);
char *reverse(char*);
main() {
printf("%d", length("himanshupal"));
printf("%s", reverse("himanshupal"));
getch();
}
/* used to calculate the lenghth*/
int length(char *p) {
int l;
for (l = 0; *(p+l) != '\0'; l++);
return (l);
}
char *reverse(char *p) { // function for reversing the string
// l used for length
int i, l;
char t;
for (l = 0; *(p+l) != '\0'; l++);
for (i = 0; i < l / 2; i++) {
t = *(p+i);
*(p+i) = *(p+l-1-i);
*(p+l-1-i) = t;
}
return (p);
}
String literals are immutable in C and C++. Any attempt to modify a string literal results in undefined behavior.:)
Use character arrays instead. for example
int main( void )
//^^^^^^^^^^^^^^
{
char s[] = "himanshupal";
printf( "%d\n", length( s ) );
printf( "%s\n", reverse( s ) );
getch();
}
Take into account that it is better to declare the function length like
size_t length( const char * );
I am writing a code to remove the repeated occurrence of a character in the string.
Description:- Remove the repeated characters from the string
Example:-
Sample Input = abcdeabd
Sample Output =abcde
I have written the code and it is working and when I tested by running sample test cases ,It is passing most of the test cases but is failing some e.g. when I Use the input string as "abcdabcdabcdabcd" It is giving me abcdd as the output instead of "abcd"
Here is my code
#include<stdio.h>
int main(void)
{
char a[60]="abcdeabd";
int n=0;
for(int l=0;a[l]!='\0';++l)
++n;
printf("%d\n",--n);
for(int i=0;i<=n;++i)
{
for(int j=i+1;j<=n;++j)
{
if(a[i]==a[j])
{
for(int k=j;k<=n;++k)
a[k]=a[k+1];
--n;
}
}
}
puts(a);
return 0;
}
Please tell me where I am going wrong with this code...?
The logic error is in the block
if(a[i]==a[j])
{
for(int k=j;k<=n;++k)
a[k]=a[k+1];
--n;
}
It doesn't work when you have the same character more than twice in succession. It doesn't work for `"addd" or "adddbc".
Change that to a while loop to fix the problem.
while (a[i] == a[j])
{
for(int k=j;k<=n;++k)
a[k]=a[k+1];
--n;
}
As for me I would write a corresponding function using pointers. For example
#include <stdio.h>
char * unique( char *s )
{
char *last = s, *current = s;
do
{
char *t = s;
while ( t != last && *t != *current ) ++t;
if ( t == last )
{
if ( last != current ) *last = *current;
++last;
}
} while ( *current++ );
return s;
}
int main(void)
{
char s[]="abcdeabd";
puts( s );
puts( unique( s ) );
return 0;
}
The output is
abcdeabd
abcde
As for your code then I would rewrite it the following way Take into account that you have to copy also the terminating zero.
#include <stdio.h>
char *unique( char *s )
{
int n = 0;
while ( s[n++] != '\0' );
printf( "%d\n", n );
for ( int i = 0; i < n; ++i )
{
for ( int j = i + 1; j < n; ++j )
{
if ( s[i] == s[j] )
{
--n;
for ( int k = j; k < n; ++k ) s[k] = s[k+1];
}
}
}
return s;
}
int main(void)
{
char s[]="abcdeabd";
puts( s );
puts( unique( s ) );
return 0;
}
#include<stdio.h>
int main(void)
{
char a[10]="abcdeabd";
for(int i=0;a[i]!='\0';++i)
printf("\n %c", a[i]);
for(i=0;a[i]!='\0';++i)
for(int j=i+1;a[j]!='\0';++j)
if(a[i]==a[j])
for(int k=j;a[k]!='\0';++k)
a[k]=a[k+1];
puts(a);
return 0;
}
#Patel: Your program is correct.
You missed only one single thing.
When You used printf(), you decrement the value of n.
So put a line to increment it after printf()
++i;