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
Related
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){...}
This question already has answers here:
Difference between scanf("%c", &c) and scanf(" %c", &c) [duplicate]
(3 answers)
Reading a character with scanf_s
(3 answers)
Closed 4 years ago.
In the main function if i simply declare the replacement character like this..
c = '*';
the program will keep looping normal and work normal after user enters y or Y to proceed.
But if i want the user to enter the replacement character and do this..
c = getchar();
after the user enters either y or Y, the loop ends and the program just closes.
Why is this?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BUFF_SIZE 512
void getRandomStr(char s1[]);
void strreplace(char s1[], char chrs[], char c);
void check(char s2[], char chrs[]);
char cont(void);
int main()
{
char s1[BUFF_SIZE];
char s2[BUFF_SIZE], c;
char proceed = 0;
do {
getRandomStr(s1);
printf("Your random string is: %s\n", s1);
/* gets(s1) */
printf("\nPlease enter up to 20 letters to be replaced: ");
gets(s2);
printf("\nPlease enter a replacement character (Ex. *, $, etc.): ");
c = getchar();
check(s1, s2);
printf("\nModified string after replacement is: ");
strreplace(s1, s2, c);
proceed = cont();
} while (proceed == 'Y' || proceed == 'y');
}
void getRandomStr(char s1[]) {
int i;
srand(time(NULL));
for (i = 0; i < 41; i++) {
char c = rand() % 26 + 'A';
s1[i] = c;
}
s1[41] = '\0';
}
void strreplace(char s1[], char chrs[], char c)
{
int i = 0;
while (chrs[i] != '\0') {
for (int j = 0; s1[j] != '\0'; j++) {
if (s1[j] == chrs[i])
{
s1[j] = c;
}
}
i++;
}
puts(s1);
}
char cont()
{
char proceed;
printf("\nWould you like to run the program again (y/n)? ");
scanf_s("%c%*c", &proceed);
return proceed;
}
void check(char s2[], char chrs[])
{
int i = 0;
while (chrs[i] != '\0') {
for (int j = 0; s2[j] != '\0'; j++) {
if (!(chrs[i] >= 'A' && chrs[i] <= 'Z'))
{
printf("An invalid character was entered.\n");
break;
}
}
i++;
}
}
Do anybody knows how to search for a name in a string array? If i register the name 'jacob' and search for cob I need to get jacob shown instead of not showing up anything. I don't know if strcmp is the right way to do it. Any ideas?
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 20
struct name{
char name[MAX];
};
void getRegister(struct name name[], int *nrNames);
void getSearch(struct name name[], int nrNames);
int readLine(char s[], int length);
int main(){
int run=1;
struct name name[MAX];
int nrNames=0;
while(run){
char choice;
printf("\n (1)Register\n(2)Search\n(3)Quit\n");
scanf(" %c%*c", &choice);
if(choice=='1') getRegister(name, &nrNames);
if(choice=='2') getSearch(name, nrNames);
if(choice=='3') run=0;
}
return 0;
}
void getRegister(struct name name[], int *nrNames){
char input[MAX];
printf("Enter name: ");
readLine(input, MAX);
(*nrNames)++;
}
void getSearch(struct name name[], int nrNames){
int i;
char input[MAX];
printf("Enter name: ");
readLine(input, MAX);
if(i>=0){
printf("Name/s:\n");
for(i=0; i<nrNames;i++){
if(strcmp(input, name[i].name)==0){
printf("\n%s\n",name[i].name);
}
}
}
}
int readLine(char s[], int length){
int ch, i=0;
while(isspace(ch=getchar()));
while(ch!='\n' && ch!=EOF) {
if(i<length) s[i++]=ch;
ch = getchar();
}
s[i]='\0';
return i;
}
Try to search for the match in the array. The code below displays a position for each occurrence of the second array in the first array. It uses naive approach. There are more efficient algorithms like Knuth-Morris-Pratt or Boyer-Moore algorithm.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 20
int main(){
char c;
char name[MAX], search_name[MAX];
int i = 0, j = 0, match = 0, count = 0;
printf("Register name: ");
while ((c = fgetc(stdin)) != '\n') {
if (i < MAX){
name[i++] = c;
}
}
name[i] = '\0';
printf("Search name: ");
i = 0;
while ((c = fgetc(stdin)) != '\n') {
if (i < MAX){
search_name[i++] = c;
}
}
search_name[i] = '\0';
i=-1;
match = 0;
do {
i++;
j = 0;
do {
if (name[i+j] == search_name[j])
match = 1;
else {
match = 0;
break;
}
j++;
} while (search_name[j] != '\0');
if (match)
printf("Match on position %d ", i);
} while (name[i+j] != '\0');
printf("\n");
return 0;
}
I found the soulution myself. Here is the code for those who get stuck as I did.
void searchName(const struct varor reg[], int nrOfGoods){
int i;
char name[20];
printf("Enter name: ");
readLine(name, WORDLENGTH);//gets input
if(i>=0){
printf("\nId.number \t Name \t\t\t Quantity\n");
for(i=0; i<nrOfGoods;i++){
if(strstr(reg[i].name, name)!=NULL){ //this should do the job
printf("%-17d%-24s%-5d\n",reg[i].idnumber,reg[i].name.reg[i].quantity);
}
}
}
}
I wanted the two functions to stop the program as the word "exit" is entered, but the loop for that specification is not working. can you please fix this?
Here is the code. please check it.
#include<stdio.h>
#include<string.h>
void uppercase(char *str) {
int i;
for(i=0; i<=strlen(str); i++) {
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
printf("\nThe string in lower case is->%s\n",str);
}
void lowercase(char *str) {
int i;
for(i=0; i<=strlen(str); i++) {
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf("The uppercase equivalent is: %s\n", str);
getchar();
}
int main()
{
char str[100];
char lower[100];
int i;
while(1)
{
printf("Enter any string->");
scanf("%s",&str);
if (str == 'exit')
{
break;
}
else
{
printf("The string is->%s\n",str);
uppercase(str);
lowercase(str);
}
}
return 0;
}
replace:
if (str == 'exit')
by this
if (strcmp(str , "exit")==0)
void uppercase(char *str){
int i;
for(i=0;i<strlen(str);i++){
//if(islower(str[i]))
str[i]=toupper(str[i]);
}
printf("\nThe string in uppercase is->%s\n",str);
}
I am trying to create a program that can modify a text according to the user key. It seems to work well, until I input something and it adds extra things.
For example, if I add the word hello and a key of 3, it says khoor plus some extra weird characters. Can you tell me please what is the problem? Thank you very much.
#include <stdio.h>
#include <ctype.h>
#define MAXSIZE 100
void encrypt(senTence[], int key);
int main(void)
{
int userKey;
char sentence[MAXSIZE];
printf("Input the text that you want to encrypt:\n> ");
fgets(sentence, 99, stdin);
// printf("\nThe string that you wrote is:\n%s\n\n", sentence);
printf("Input the key:\n");
scanf("%d", &userKey);
//printf("\nThe key that you selected is: %d\n\n", userKey);
encrypt(sentence, userKey);
return 0;
}
void encrypt(const char senTence[], int key)
{
int i = 0;
char q[MAXSIZE];
for(i = 0; senTence[i] != '\0'; ++i)
{
if( ( isupper(senTence[i]) ) || ( islower(senTence[i]) ) )
{
q[i] = senTence[i] + (char)key;
}
else
{
q[i] = (senTence[i]);
}
}
printf("%s", q);
}
You didn't terminate the result string q in encrypt().
Add the following line before printf().
q[i] = '\0';
Another way is initialize q to all-zero:
char q[MAXSIZE] = {0};
You forgot to null terminate your array q, so using as a string will not be possible.
After you have performed the required operation on all the elements of the senTence and stored it to q, you need to null terminate q.
Use
q[i] = '\0';
printf("%s\n", q);
I ran the code, was giving a few warnings and an error, related to the function prototype. I fixed that and it is working fine now!
#include <stdio.h>
#include <ctype.h>
#define MAXSIZE 100
void encrypt(const char senTence[], int key);
int main(void)
{
int userKey;
char sentence[MAXSIZE];
printf("Input the text that you want to encrypt:\n> ");
fgets(sentence, 99, stdin);
// printf("\nThe string that you wrote is:\n%s\n\n", sentence);
printf("Input the key:\n");
scanf("%d", &userKey);
//printf("\nThe key that you selected is: %d\n\n", userKey);
encrypt(sentence, userKey);
return 0;
}
void encrypt(const char senTence[], int key)
{
int i = 0;
char q[MAXSIZE];
for(i = 0; senTence[i] != '\0'; ++i)
{
if( ( isupper(senTence[i]) ) || ( islower(senTence[i]) ) )
{
q[i] = senTence[i] + (char)key;
}
else
{
q[i] = (senTence[i]);
}
}
printf("%s", q);
}