I created a program in which i declared two arrays of char type. One would store the string and another would store the reversed string.But when i print the second array it displays some unexpected results.
#include<stdio.h>
main(){
char a[] = "Sahib";
char b[5];
int i = 0;
int j,k = 0;
char c='a';
while(c!='\0'){
c = a[i];
i++;
}
i -= 2;
for(j=i;j<=0;j--){
b[k] = a[j];
k++;
}
printf("The reversed character is %s",b);
}
try this:
#include <stdio.h>
int main(void){
char a[] = "Sahib";
char b[sizeof a];
int i = 0;
int j,k = 0;
char c;
while((c = a[i]) != '\0'){
i++;
}
for(j = --i; j>=0; --j){
b[k++] = a[j];
}
b[k] = '\0';
printf("The reversed character is %s\n", b);
return 0;
}
Related
I have written below program to reverse the string, but it is not printed after reversal.
What could be the issue?
#include<stdio.h>
#include<string.h>
main()
{
char p[] = "krishna";
strrv(p);
printf("%s", p); // -----> nothing prints here
}
void strrv(char p[])
{
int l = strlen(p);
int i=0;
char tmp;
while(i<l)
{
tmp = p[i];
p[i] = p[l];
p[l] = tmp;
i++;
l--;
}
}
On the first loop iteration p[l] will refer to the terminating \0 of p which is then assigned to p[0] and that in turn that makes p an empty string. The fix is to initialize l to strlen(p) - 1 instead of strlen(p):
#include <stdio.h>
#include <string.h>
void strrv(char p[]) {
for(int i = 0, l = strlen(p) - 1; i < l; i++, l--) {
char tmp = p[i];
p[i] = p[l];
p[l] = tmp;
}
}
int main() {
char p[] = "krishna";
strrv(p);
printf("%s", p);
return 0;
}
I am writing a code for the function maxCharToFront() that accepts a character string str as parameter, finds the largest character from the string (based on ASCII value), and moves it to the beginning of the string.
Main function:
#include <stdio.h>
#include <string.h>
void maxCharToFront(char *str);
int main()
{
char str[80], *p;
printf("Enter a string: \n");
fgets(str, 80, stdin);
if (p=strchr(str,'\n')) *p = '\0';
printf("maxCharToFront(): ");
maxCharToFront(str);
puts(str);
return 0;
}
This is the function using for loop:
void maxCharToFront(char *str)
{
int i, maxcharint=0, maxindex=0;
char maxchar;
for (i=0; str[i] != '\0'; i++){
if ( (int)str[i]> maxcharint){
maxcharint = (int)str[i];
maxindex = i;
}
i++;
}
for (i = maxindex-1; i>-1; i--){
str[i+1]= str[i];
}
maxchar = maxcharint;
str[0]= maxchar;
}
when I enter ab as input, it gives back ab as output for for loop.
This is the function using while loop:
void maxCharToFront(char *str)
{
int i=0, maxindex=0, maxcharint=0;
char maxchar;
while (str[i]!='\0'){
if ((int)str[i]>maxcharint){
maxindex = i;
maxcharint = (int)str[i];
}
i++;
}
for(i=maxindex-1; i>-1; i--){
str[i+1]=str[i];
}
maxchar = maxcharint;
str[0] = maxchar;
}
When I enter ab as input, it successfully gives back ba as output for while loop.
However, I don't see a difference in my while and for loop since both transverses all the characters in the input string starting from str[0]. Why is there an output difference? Thank you.
in for loop you increment i twice, once in for loop and once more later on i++
void maxCharToFront(char *str)
{
int i, maxcharint=0, maxindex=0;
char maxchar;
for (i=0; str[i] != '\0'; i++/*increment here*/){
if ( (int)str[i]> maxcharint){
maxcharint = (int)str[i];
maxindex = i;
}
i++; // increment twice
}
for (i = maxindex-1; i>-1; i--){
str[i+1]= str[i];
}
maxchar = maxcharint;
str[0]= maxchar;
}
how to code a user defined function that searches and replaces a character occurrences of any of the character contained in another string with a character string.
Cannot used any string variable in the code, has to be a user defined function.
Thanks
This is what i have tried so far
#define _CRT_SECURE_NO_WARNINGS
#include
#include
void s1();
void s2();
int main(void)
{
int i=0;
s1();
s2();
printf("c = {'$'} ");
}//main
void s1(){
int i = 0;
while (i <= 40){
printf("%c", (rand() % 25) + 'A');
i++;
}
}
void s2(){
char s2[20];
printf("\nEnter a string of minimum 2 and maximum 20 characters= ");
gets(s2);
puts(s2);
}
/*
I just need to make another function that searches s1 and replaces any occurrence of any of the character contained is s2 with a character that can be anything(e.g. '$')
*/
//If I have understood your question then this should be answer
char *replace(char [] a, char b[], int lower, int upper){
char c[100];
int j = 0;
for(int i = 0; i < lower; i++){
c[j] = a[i];
j++;
}
for(int i = 0; i < strlen(b); i++){
c[j] = b[i];
j++;
}
for(int i = upper; i < strlen(a); i++){
c[j] = a[i];
j++;
}
c[j] = '\0'
for(int i = 0; i < strlen(c); i++){
a[i]= c[i];
}
a[i] = '\0';
return a;
}
I want to divide *eString to substrings. Substrings should be like that:
y_{1} = y_{1}y_{m+1}y_{2m+1}...
y_{2} = y_{2}y_{m+2}y_{2m+2}...
y_{m} = y_{m}y_{2m}y_{3m}...
where y is the element of *eString, and y is the substring of these elements.
For instance, if an user expects the key length which is 5, there should be (string size / 5) substrings. y_{1} has to contain the fist element of each divided substring. So, how can I implement this?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ALPHA 26
char *ReadFile(char *);
int main(int argc, char *argv[])
{
double frequency[ALPHA] = {0};
int c = 0;
int keylen = 0;
int counter = 0;
double indexofCoincidence = 0,total = 0;
const char *eString = ReadFile("cipher.txt");
int len = 0;
if (eString) {
puts("The encrypted text is:");
puts(eString);
puts("");
len = strlen(eString);
printf("The length of text is %d\n",len);
}
puts("");
while(eString[c]!= '\0'){
if(eString[c]>= 'a' && eString[c]<='z')
frequency[eString[c]-'a']++;
c++;
}
puts("The letters frequencies are :\n");
for(c=0; c<ALPHA;c++){
if(frequency[c]!= 0)
printf("%c : %.3f\t",c+'a',(frequency[c]/len));
total += (frequency[c]*(frequency[c]-1));
}
indexofCoincidence = (total/((len)*(len-1)));
printf("\n\nIndex of Coincidence : %.3f\n",indexofCoincidence);
if(indexofCoincidence < 0.060){
printf("\nIt looks like randomly.\n");
}
printf("Enter the your expected key length : ");
scanf("%d",keylen);
printf("\n");
char *y;
while(counter != keylen)
{
for(int i = 0; i<(len/keylen);i++){
y[counter] = *eString();
}
counter++
}
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
char *eString = "The quick brown fox jumps over the lazy dog";
int keylen = 5;
int len = strlen(eString);
int y_len = (len + keylen) / keylen + 1;
int i,j;
char **y = malloc(keylen * sizeof(*y));
for(i=0; i < keylen; ++i){
y[i] = malloc(y_len * sizeof(**y));
}
char *p = eString;
i = j = 0;
while(*p){
y[i % keylen][j] = *p++;
y[i % keylen][j+1] = 0;
if(++i % keylen == 0)
++j;
}
//check print & deallocate
for(i = 0; i < keylen; ++i){
printf("y_{%d} : %s\n", i+1, y[i]);
free(y[i]);
}
free(y);
return 0;
}
I just want to reverse a string using for loop and array. Don't want to use any predefined function. I used the following code but its near to nothing. Please share some good suggestions.
int main(){
char a[]="this is a man";
char b[30];
int p= sizeof(a)/sizeof(a[0]);
for(int i=p-1;i>0;i--){
for(int j=0;j<p;j++){
b[j]=a[i];
}
}
printf("array is %s",b);
return 0;
}
#include<stdio.h>
int main(){
char str[] = "str to rev";
char revstr[12]={'\0'};
int i, j;
int length = strlen(str);
j = 0;
for(i = length-1; i>=0; i--){
revstr[j] = str[i];
j = j + 1;
}
printf("%s", revstr);
return 0;
}
1) In your first for loop, you have to reach 0 (i>=0)
for(int i=p-1;i>=0;i--){
2) The a[p-1] contains the null termination('\0') of your string a[]. And the null termination should not be included in the array reverse procedure. So in your first loop you should start from p-2 and not from p-1.
And after finishing the reversing you have to add a '\0' (null terminator) at the end of your b array
b[j]='\0'; // add this
printf("array is %s",b);
return 0;
3) And as said in the other answers, you have to use only one loop and not 2 loops.
int i,j;
for (i=p-2, j=0; i>=0; i--,j++) {
b[j]=a[i];
}
b[j]='\0';
printf("array is %s",b);
Using while loop::
void main()
{
char str[100],temp;
int i,j=0;
printf("nEnter the string :");
gets(str);
i=0;
j=strlen(str)-1;
while(i<j)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
i++;
j--;
}
printf("nReverse string is :%s",str);
return(0);
}
Using for loop::
void StrRev(char *str)
{
int i, len, endpos;
len = strlen(str);
endpos = len-1;
for(i = 0; i < len / 2; i++)
{
char temp = str[i];
str[i] = str[endpos - i];
str[endpos - i] = temp ;
}
}