I need to find all suffix starting with a character X. For example, for int suffix (char str [], char c) when the word is ababcd and the letter b it should return:
babcd
bcd
and the number 2.
This is my code:
#include <stdio.h>
#include <string.h>
int main()
{
char c;
char str[128];
int counter=0;
printf ("Please enter charachter and a string \n");
scanf("%c %s",&c,str);
counter = my_suffix(str,c);
printf("The string has %d suffix \n",counter);
return 0;
}
int my_suffix(char str[],char c) {
int counter = 0;
for (int i=0; i < strlen(str); i++)
{
if (str[i] == c)
{ puts(str+i);
counter++;
}
}
return counter;
}
I couldn't find why it's not running,
Thanks!
Your code is fine you should just written following method above int main()
int my_suffix(char str[],char c){...}
Related
I tried to write a function that inserts space at regular intervals in a string.
If a[50] is a string, and n is the preferred interval from the user,
insert_space(a,b,len,n) will insert blanks after the n'th column and will store the modified string in b.
#include <stdio.h>
int getinput(char temp[]);
void insert_space(char s1[],char s2[],int,int);
int main ()
{
int n, len;
char a[100], b[100];
printf("Enter the nth column number for inserting\n");
scanf("%d",&n);
printf("Enter the line\n");
len=getinput(a);
insert_space(a,b,len,n);
printf("%s\n",b);
}
void insert_space(char s1[],char s2[],int len, int n)
{
int i=0, c=0,flag=0;
for(i=0;i<=len;i++)
{
if(flag!=n)
{
s2[c]=s1[i];
c++;
flag++;
}
else
{
s2[c]=' ';
i=i-1;
c++;
flag=0;
}
}
s2[c]='\0';
}
int getinput(char temp[])
{
int c, i=0;
while((c=getchar())!=EOF)
{
temp[i]=c;
i++;
}
i--;
temp[i]='\0';
return i;
}
I entered the values of the string a as abcdefghijkmnop. Instead of
"abdce fghij kmnop" as the ouput in b, I got "abcd efghi jkmno p" as the output. I'm not sure what I did wrong here.
edit: After just including the insert_function code, I've edited it to include the entire execution code.
There is a \n ,newline (Enter) from scanf("%d",&n); which is recorded as a[0]. So you have to manage this UN-handled newline (Enter).
To solve this, add an extra c = getchar(); before loop while ((c = getchar()) != EOF) in function int getinput(char temp[]), to handle that extra newline left behind by scanf("%d",&n);
Modified code:-
#include <stdio.h>
int getinput(char temp[]);
void insert_space(char s1[], char s2[], int, int);
int main()
{
int n, len;
char a[100], b[100];
printf("Enter the nth column number for inserting\n");
scanf("%d", &n);
printf("Enter the line\n");
len = getinput(a);
insert_space(a, b, len, n);
printf("%s\n", b);
}
void insert_space(char s1[], char s2[], int len, int n)
{
int i = 0, c = 0, flag = 0;
for (i = 0; i <= len; i++)
{
if (flag != n)
{
s2[c] = s1[i];
c++;
flag++;
}
else
{
s2[c] = ' ';
i = i - 1;
c++;
flag = 0;
}
}
s2[c] = '\0';
}
int getinput(char temp[])
{
int c, i = 0;
c = getchar(); // to handle extra newline from scanf
while ((c = getchar()) != EOF)
{
temp[i] = c;
i++;
}
i--;
temp[i] = '\0';
return i;
}
Output :-
Enter the nth column number for inserting
5
Enter the line
abcdefghijkmnop
abcde fghij kmnop
I've got an assignment from school to make a program that will encrypt and decrypt a text. I have to use this declaration:
int encrypted(char *plainText, int arrLength, int key, char *cipherText);
For the moment i can make the caesar cipher work when i have the for-loop (the one i show in myfunctions.c) in main.c, but when i write the for-loop in another file (myfunctions.c) with the declaration above, it compiles and runs, but it seems like the for-loop in myfunctions.c doesn't executes like it should.
Here is my main.c:
#include <stdio.h>
#include <string.h>
#include "myfunctions.h"
int main(){
int key, arrLength, menu=0;
char plainText[100], cipherText[100], result[100];
printf("Encrypt\n");
printf("Enter your key (1-25): ");
scanf("%d", &key);
printf("Write the word or sentece you want to encrypt: ");
fgets(plainText, 100, stdin);
arrLength=strlen(plainText);
encrypted(plainText, arrLength, key, result);
getchar();
return 0;
}
myfunctions.c:
#include "myfunctions.h"
#include <stdio.h>
#include <string.h>
int encrypted(char *plainText, int arrLength, int key, char *cipherText){
int result = 0;
for(int i = 0; i < arrLength; i++)
{
// encryption
result = (*plainText + key);
// wrapping after Z for uppercase letters
if (isupper(*plainText) && (result > 'Z'))
{
result = (result - 26);
}
// wrapping after z for lowercase letters
if (islower(*plainText) && (result > 'z'))
{
result = (result - 26);
}
if (isalpha(*plainText))
{
printf("%c", result);
}
else
{
printf("%c", *plainText);
}
}
return 1;
}
myfunctions.h
#ifndef myfunctions_h
#define myfunctions_h
int encrypted(char *plainText, int arrLength, int key, char *cipherText);
#endif
You forgot to inclement plainText in the for loop in encrypted().
Be careful not to have fgets() read newline character before the plain text.
Try this main function
int main(){
int key, arrLength, menu=0;
char keyText[100],plainText[100], cipherText[100], result[100];
printf("Encrypt\n");
printf("Enter your key (1-25): ");
fgets(keyText, 100, stdin);
sscanf(keyText, "%d", &key);
printf("Write the word or sentece you want to encrypt: ");
fgets(plainText, 100, stdin);
arrLength=strlen(plainText);
encrypted(plainText, arrLength, key, result);
return 0;
}
and changing the loop for(int i = 0; i < arrLength; i++)
to for(int i = 0; i < arrLength; i++, plainText++)
I need to convert an ascii input to hex input. I am very bad with C so if you could include some explanation that would be very helpful. This code is just a bunch of bits and pieces but most is probably wrong or useless. Afterwards i need to use user input to select the string but the hard part is getting it to convert at all.
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
void crypt(char *buf, char *keybuf, int keylen) {
//This is meant to encrypt by xor-ing with the sentence and key entered//
//It is also supposed to replace the original buf with the new version post-xor//
int i;
int *xp;
xp=&i;
for(i=0; i<keylen; i++) {
buf[i]=buf[i]^keybuf[i];
xp++;
}
}
int convertkey(char *keybuf) {
int keylen=0;
//I need to add something that will return the length of the key by incrementing keylen according to *keybuf//
return keylen;
}
int main(int argc, char * argv[]){
char x;
char *xp;
xp = &x;
char a[47];
char *ap;
ap=a;
printf("Enter Sentence: ");
scanf("%[^\n]",a);
printf("Enter key: ");
scanf("%d",xp);
printf("You entered the sentence: %s\n",a);
printf("You entered the key: %d\n",x);
convertkey(xp);
crypt(ap,xp,x);
printf("New Sentence: %s\n",a);
return 0;
}
Such as it is, I have reorganised your posted code so at least it compiles, even if the intent is unclear. Perhaps you can take it on from here.
#include <stdio.h>
#include <stdlib.h>
// moved out of main()
void crypt(char *buf, char *keybuf, int keylen) {
int i; // added declaration
for(i=0; i<keylen; i++) { // corrected syntax and end condition
buf[i]=buf[i]^keybuf[i];
//xp++; // out of scope
}
}
// moved out of main()
int convertkey(char *keybuf) {
int keylen=0;
return keylen;
}
int main(int argc, char * argv[]){
int x=0;
int *xp;
xp = &x; // xp=&x{0};
return 0; // exit(0);
}
This is the final product I was looking for but was very poor at explaining/coding.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void crypt(char *buf, char *keybuf, int keylen) {
int i;
int length= strlen(buf)-1;
for(i=0; i<length; i++) {
buf[i]=buf[i]^keybuf[i%keylen];
printf("%c",buf[i]);
}
printf("\n");
}
int convertkey(char *keybuf) {
int i=0;
for(i=0;keybuf[i]!='\n';i++){
if(keybuf[i]>='0' & keybuf[i]<='9'){
keybuf[i]=keybuf[i]-'0';
}
else if(keybuf[i]>='a' & keybuf[i]<='f'){
keybuf[i]=(keybuf[i]-'a')+10;
}
}
return i;
}
int main(int argc, char * argv[]){
char keychars[12];
char a[48];
char *ap;
int i;
ap=a;
printf("Enter Sentence: ");
fgets(a, 48, stdin);
printf("Enter Key: ");
fgets(keychars, 12, stdin);
for (i=0; i<strlen(keychars); i++) {
char c = keychars[i];
printf("keychars[%d]=%c (character), %d (decimal), %x (hex)\n", i, c, c, c);
}
crypt(ap,keychars,convertkey(keychars));
return 0;
}
I am having a few issues with my code. First: when I try to compile, I get error: too few arguments to function 'strcmp'. I have looked all over and made multiple changes and am still unable to get it to work. Second: when my code does compile (if I remove the strcmp part), it will not complete the count functions correctly. Can anyone please assist? Thank you!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int count(char array[], int size);
int stringLen(char array[]);
int convert(char ch);
int value, n;
int main()
{
//char * str;
//char s;
char a[100];
char b[100];
char c[100];
int charCount = stringLen(a);
int lCount = count(a, charCount);
printf("Enter your string: \n");
scanf("%s \n", a);
printf("Enter your string: \n");
scanf("%s \n", b);
printf("Enter your string: \n");
scanf("%s \n", c);
printf("The count is %d, length is %d\n", lCount, charCount);
int i;
for(i = 0; i < charCount; i++)
{
char c = a[i];
printf("Char %s = %d \n", &c, value);
}
n = strcmp(char string1[], char string2[], char string3[]);
printf("The first string in the alphabet is: %d \n", n);
return 0;
}
int stringLen(char array[])
{
char count;
int index;
while(array[index] !=0)
{
count++;
index++;
}
return count;
}
int count(char array[], int size)
{
int count;
int i;
for(i = 0; i < size; i++)
{
if(array[i] == 'a')
{
count ++;
}
else if(array[i] == 'A')
{
count ++;
}
}
return count;
}
This is not right way to use strcmp.
n = strcmp(char string1[], char string2[], char string3[]);
strcmp is used for compararison of string. See doc
int result = strcmp (string1,string2)
If strings are same, function will return 0.
This code cannot convert char* to char**. I don't know what it means.
Here is my code:
#include <stdio.h>
#include <conio.h>
#include <string.h>
shift( char *s[] , int k )
{
int i,j;
char temp[50];
for( i = 0 ; i < k ; i++ )
temp[i]=*s[i] ;
for( j = 0 ; j < strlen(*s) ; j++ )
{
*s[j] = *s[k] ;
k++ ;
}
strcpy(*s,temp);
}
main()
{
int i,j=0,k;
char s[30];
printf("please enter first name ");
gets(s);
scanf("%d",&k);
shift( &s , k);
puts(s);
getch();
}
The program is supposed to:
read string S1 and index âKâ, then call your own function that rotates the string around
the entered index. The output of your program should be as follows:
Enter your string: AB3CD55RTYU
Enter the index of the element that rotates the string around: 4
The entered string: AB3CD55RTYU
Enter the element that rotates the string around: D
The rotated string is : D55RTYUAB3C
&s means char (*)[30](pointer to array of char[30]) not char *[] (array of pointer to char)
For example, It modified as follows.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void shift(char s[],int k){
int i, len;
char temp[50];
for(i=0;i<k;i++)
temp[i]=s[i];
temp[i] = '\0';
len = strlen(s);
for(i=0;k<len;i++)
s[i]=s[k++];
strcpy(&s[i],temp);
}
int main(){
int k;
char s[30];
printf("please enter first name ");
gets(s);
scanf("%d", &k);
shift(s , k);
puts(s);
getch();
return 0;
}
example using a structure(Copy is performed). However, this is waste of resources.
#include <stdio.h>
#include <conio.h>
typedef struct word {
char str[30];
} Word;
Word shift(Word word, int k){
Word temp;
int i = 0, j;
for(j=k;word.str[j]!='\0';++j)
temp.str[i++]=word.str[j];
for(j=0;j<k;++j)
temp.str[i++]=word.str[j];
temp.str[i] = '\0';
return temp;
}
int main(){
int k;
Word w;
printf("please enter first name ");
gets(w.str);
scanf("%d", &k);
w=shift(w , k);
puts(w.str);
getch();
return 0;
}
shift(char* s[],int k); //shift expects char**; remember s[] is actually a pointer
main()
{
char s[30]; // when you declare it like this s is a pointer.
...
shift(s , k);
}
You should change the shift function signature to shift(char* s,int k); since you don't really need pointer to pointer. You just need to pass the beginning of the array.